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

Mongoose 5 to 6: Virtual populate lean does not return empty array on empty match #10992

Closed
heyyyce opened this issue Nov 18, 2021 · 4 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@heyyyce
Copy link

heyyyce commented Nov 18, 2021

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

What is the current behavior?
I'm migrating mongoose from 5 to 6 and upon testing I got this issue:
When I do find() with populate() on a virtual populate and lean()
I get a return undefined from campaignConsumeMonthlyMulti (code below)

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

Here my virtual in schema

CampaignSchema.virtual('campaignConsumeMonthlyMulti', {
ref: 'campaigns_consume',
    localField: '_id',
    foreignField: 'campaignsId',
    justOne: false
});

Here the query

let campaign = await Campaign.findOne({ companyId: companyId })
.populate(
    {
        path: "campaignConsumeMonthlyMulti",
        match: {
            periodCount: { $gt: datePeriod },
            type: campaignConsumeTypeEnum.monthly.value
        }
    })
.lean();

campaign.campaignConsumeMonthlyMulti == undefined

If the match matches data it works like a charm.

What is the expected behavior?
It should return an empty array like in mongoose 5.12.11

When I remove the lean() call : campaign.campaignConsumeMonthlyMulti getter give me the empty array.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
NodeJs: 16.11.0
Mongoose: 6.0.13
MongoDB: 4.2.17

@vkarpov15
Copy link
Collaborator

This is by design. lean() should not set virtuals, you can use the mongoose-lean-virtuals plugin to get this behavior though.

We'll add a note to the changelog about this.

@vkarpov15 vkarpov15 added this to the 6.0.14 milestone Nov 24, 2021
@vkarpov15 vkarpov15 added the docs This issue is due to a mistake or omission in the mongoosejs.com documentation label Nov 24, 2021
@heyyyce
Copy link
Author

heyyyce commented Nov 25, 2021

Hello

This is by design. lean() should not set virtuals, you can use the mongoose-lean-virtuals plugin to get this behavior though.

lean() don't set virtuals() as getters() and defaults()

But here I'm using some Virtual populate :
https://mongoosejs.com/docs/populate.html#populate-virtuals

And according to the docs :
Virtual populate also works with lean.
https://mongoosejs.com/docs/tutorials/lean.html#lean-and-populate

Regards

@IslandRhythms
Copy link
Collaborator

Remember that virtuals do not end up in lean() query results. Use the mongoose-lean-virtuals plugin to add virtuals to your lean query results.

https://mongoosejs.com/docs/tutorials/lean.html#plugins

@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed docs This issue is due to a mistake or omission in the mongoosejs.com documentation labels Nov 29, 2021
vkarpov15 added a commit that referenced this issue Nov 29, 2021
@heyyyce
Copy link
Author

heyyyce commented Nov 30, 2021

Thanks you for the fix!

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