Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populating a field of the items in an array, adds the field to every item regardless of its schema #10599

Closed
rstcruzo opened this issue Aug 24, 2021 · 5 comments
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@rstcruzo
Copy link

rstcruzo commented Aug 24, 2021

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
Populating an array with multiple discriminators adds the field as null or empty array even when the item schema doesn't have the field defined. This happens when the populated field is either an array or a virtual.

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('mongoose');
const {Schema, ObjectId, model} = mongoose;

const ImageSchema = new mongoose.Schema({
    imageName: {
        type: String,
        required: true
    }
}, {_id: false});

const TextSchema = new mongoose.Schema({
    textName: {
        type: String,
        required: true
    },
    attached: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'AnotherModel'
    }]
}, {_id: false});

const ItemSchema = new Schema({
    objectType: {
        type: String,
    },
}, {
    discriminatorKey: 'objectType',
    _id: false,
});

const ExampleSchema = new Schema({
    test: {
        type: String,
    },
    list: [{
        type: ItemSchema,
        required: false
    }],
});

ExampleSchema.path('list').discriminator('Image', ImageSchema);
ExampleSchema.path('list').discriminator('Text', TextSchema);
const Example = model('Example', ExampleSchema);

const example = await Example.create({
    test: 'example',
    list: [
        {
            imageName: "an image",
            objectType: 'Image',
        },
        {
            textName: "this is a text",
            attached: ["61254490ea89de0004c8f2a0"],
            objectType: 'Text',
        },
    ],
});
const query = Example
    .findById(example._id)
    .populate('list.attached')
    .lean()

const result = await query.exec();

return result;

What is the expected behavior?
result should be:

const result = {
    test: 'example',
    list: [
        {
            imageName: "an image",
            objectType: 'Image',
        },
        {
            textName: "this is a text",
            attached: [{field: 'value'}],
            objectType: 'Text',
        },
    ],
}

result is:

const result = {
    test: 'example',
    list: [
        {
            attached: [],  // extra field. Image doesn't have a field called attached.
            imageName: "an image",
            objectType: 'Image',
        },
        {
            textName: "this is a text",
            attached: [{field: 'value'}],
            objectType: 'Text',
        },
    ],
}

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Nodejs: 12.21.0
mongoose: 5.12.1
mongodb: 3.2.22

@vkarpov15 vkarpov15 added this to the 5.13.9 milestone Aug 24, 2021
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Aug 24, 2021
@IslandRhythms
Copy link
Collaborator

@rstcruzo could you please include what AnotherModel is? I'm reproing it right now and want to make sure that AnotherModel doesn't affect the output.

@IslandRhythms
Copy link
Collaborator

this is result {
  _id: new ObjectId("61295305a763d02001b053f4"),
  test: 'example',
  list: [
    { imageName: 'an image', objectType: 'Image', attached: [] },
    { textName: 'this is a text', attached: [], objectType: 'Text' }
  ],
  __v: 0
}

I'm not getting the same thing you are. I don't know if that has to do with schema for AnotherModel

@rstcruzo
Copy link
Author

rstcruzo commented Aug 27, 2021

@IslandRhythms thanks for looking into this. That's exactly what I get. The image item now contains the attached field even though its schema doesn't have it.

@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Aug 27, 2021
@rstcruzo
Copy link
Author

@vkarpov15 Thanks! That fixed it for regular fields, not for virtuals though. Should we reopen the issue?

@vkarpov15
Copy link
Collaborator

@rstcruzo can you please clarify what you mean by "not for virtuals"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants