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

Document match and other populate() options as options to Schema#virtual() and VirtualType constructor #8616

Closed
dar097 opened this issue Feb 24, 2020 · 3 comments
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Milestone

Comments

@dar097
Copy link

dar097 commented Feb 24, 2020

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

Code snippet for use in explanations
Model.populate({ path: 'items', match: { status: 'purchased' }, as: 'purchased_items' }).populate({ path: 'items', as: 'all_items' })

What is the current behavior?
Using the above code snippet, the 'as' is ignored, and the last populate overrides the previous one, resulting in all items being returned under the name 'items'.

What is the expected behavior?
By providing the 'as' property, each populate is returned under its defined 'as' value, items with the status of 'purchased' under the property 'purchased_items' and all items under the property 'all_items'. This ideally would also work with virtual populate, allowing also count operations to be separable.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js: 10.16.0
Mongoose: 5.8.4
Mongodb: 3.4.1

@dar097 dar097 changed the title Feature Request: Populate as in populate function Feature Request: Populate 'as' in populate function Feb 24, 2020
@vkarpov15
Copy link
Collaborator

Why not just have purchased_items and all_items as populate virtuals?

schema.virtual('purchased_items', {
  ref: 'Item',
  localField: 'items',
  foreignField: '_id',
  justOne: false,
  match: { status: 'purchased' }
});

schema.virtual('all_items', {
  ref: 'Item',
  localField: 'items',
  foreignField: '_id',
  justOne: false
});

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Feb 27, 2020
@dar097
Copy link
Author

dar097 commented Feb 28, 2020

Why not just have purchased_items and all_items as populate virtuals?

schema.virtual('purchased_items', {
  ref: 'Item',
  localField: 'items',
  foreignField: '_id',
  justOne: false,
  match: { status: 'purchased' }
});

schema.virtual('all_items', {
  ref: 'Item',
  localField: 'items',
  foreignField: '_id',
  justOne: false
});

I was under the impression that match was not usable in virtual population, I'll try it out when I can.. If it is possible, is there any documentation of everything else that is possible here?

@vkarpov15
Copy link
Collaborator

You can use match with virtual populate. In general, conventional populate and virtual populate support the same options. The API docs have a list of all options for populate().

We need to add these options to the VirtualType constructor's API docs.

@vkarpov15 vkarpov15 changed the title Feature Request: Populate 'as' in populate function Document match and other populate() options as options to Schema#virtual() and VirtualType constructor Mar 5, 2020
@vkarpov15 vkarpov15 added this to the 5.9.4 milestone Mar 5, 2020
@vkarpov15 vkarpov15 added docs This issue is due to a mistake or omission in the mongoosejs.com documentation and removed help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary labels Mar 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This issue is due to a mistake or omission in the mongoosejs.com documentation
Projects
None yet
Development

No branches or pull requests

2 participants