I am trying to populate array of id's in aggregation method using $lookup in mongoose. I am using mongoose 6.6.2. Here is how I am storing data in mongoDB
and this is the query I am using
await mongoose.models.xyz.aggregate([
{
$lookup: {
from: '$dietaries',
localField: 'dietaries',
foreignField: '_id',
as: 'dietaries'
}
}
]);
I am getting empty array as return.
CodePudding user response:
I'm guessing the problem is with this line from: '$dietaries'
If your collection is named "dietaries", it should be from: 'dietaries'
. You don't need the $
there.