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

Define populate virtuals on document arrays on the document array, not top-level document #8210

Open
vkarpov15 opened this issue Oct 3, 2019 · 0 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature

Comments

@vkarpov15
Copy link
Collaborator

Re: #8198 , it looks like if you do:

const FooSchema = new Schema({
  name: String,
  children: [{
    barId: { type: Schema.Types.ObjectId, ref: 'Bar' },
    quantity: Number,
  }],
}, {
  toJSON: { virtuals: true },
  toObject: { virtuals: true },
});
FooSchema.virtual('children.bar', {
  ref: 'Bar',
  localField: 'children.barId',
  foreignField: '_id',
  justOne: true,
});

children.bar is a top-level virtual and populated virtuals live on the top-level document, leading to weird issues if you access the virtual using children[0].bar as opposed to get('children.0.bar').

@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Oct 3, 2019
@vkarpov15 vkarpov15 added the enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature label Oct 3, 2019
vkarpov15 added a commit that referenced this issue Oct 3, 2019
vkarpov15 added a commit that referenced this issue Mar 22, 2023
…quivalent to creating virtual on the subdocument

Fix #13189
Re: #8210
Re: #8198
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature
Projects
None yet
Development

No branches or pull requests

1 participant