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
Aggregated subdocuments is not populated with Model.populate(docs, opts). #10978
Comments
There are three problems here. First, you're calling populate on the
Second, you don't seem to be printing the actual result of your aggregation. Calling populate doesn't modify the original object. Instead, it returns a populated object. Running the following code should achieve what you want to do: const found = await Test.aggregate([
{
$project: {
users: '$users',
},
},
{ $unwind: `$users` },
]);
console.log("Found", found);
const populated = await Test.populate(found, { path: 'users.user' });
console.log(util.inspect(populated, false, null, true)) The last problem is that you're not passing the aggregation result into the population directly. Instead, you're mapping the aggregation results into another array. I'm not sure why this prevents the aggregation from working, probably because it doesn't retain any schema information anymore, however, moving it to after the population works as intended for me. const found = await Test.aggregate([
{
$project: {
users: '$users',
},
},
{ $unwind: `$users` },
]);
console.log("Found", found);
const populated = await Test.populate(found, { path: 'users.user' });
const userObject = found.reduce(
(obj, x) => ({
count: obj.count + 1,
users: [...obj.users, x.users],
}),
{ users: [], count: 0 }
);
console.log(util.inspect(userObject, false, null, true)) |
Thanks for the help. I tried moving my population right after the aggregation instead, before the mapping. This seems to not have worked for my case. I'll try some more to find a way around, and add more comment to this post. I always find a way, just hoping this was an actual bug to keep my code clean. Comment to next line, this seems that it should be the case. But all my other test seems to modify the documents directly also.
Back to my issue, thought it was strange that this worked in any other case I was using it. That's why I added it as a bug/feature Been using mongoose for a while and always had to use the Model I wanted to populate the data with. Think this was the case when I wanted to populate the data 2-3 layers down in arrays and only wanted to populate on item instead of them all on find().populate(..) example of a Model I have
I have some logic that would filter products before populating the data. The list can be up to 100 different products, so to reduce data fetched from DB I did all the logic then only populate the category I want to get out. To get this to work I had to use something like this
I do allot of mapping in the logic and copy of items with {...}, so pretty sure the schema information connected to the document is no longer there after I'm done. If I tried using the List model it would not populate or give me null in the product or producer fields.
This may have been fixed in 6.x but I'm still running "5.13.8". Tried to upgrade, but the new changes in versions after has some major changes to TS for my code and need to get the time to upgrade. Would upgrading to 6 break my way of populating?
I'll add more to my original problems and what code caused my issue.
My ModeSearchPopulate whould look something like this. Using TS with keyof T to type valid fields and subfields to populate.
|
Fix will be in 6.0.14. As a workaround in 6.0.13, you can do |
Do you want to request a feature or report a bug?
Bug / feature
What is the current behavior?
Aggregated subdocuments is not populated with Model.populate(docs, opts).
Using aggregate to query subdocuments and want to populate data after its been fetched.
If this change is added I can use the same function to populate post fetch on single and multiple documents.
My main code seems to query the wrong model with this setup. used debug: true
schema:
user: { type: Schema.Types.ObjectId, ref: 'OrganizationUser' }
OrganizationUser.populate(docs, { path: 'user' })
->users.find({ _id: { '$in': [ new ObjectId("611a8052425e485f982e14c7") ], [Symbol(mongoose#trustedSymbol)]: true }}, { skip: undefined, limit: undefined, perDocumentLimit: undefined, projection: { _id: 1 }})
If the current behavior is a bug, please provide the steps to reproduce.
This code will not query and models with .populate as with my main code.
What is the expected behavior?
Document or documents should be populated with data with the provided Model used for population.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node.js 14.16.1
MongoDB 4.4.4
Mongoose: 6.0.13
The text was updated successfully, but these errors were encountered: