Home > Mobile >  Populating array of Id's with aggregation in mongoose
Populating array of Id's with aggregation in mongoose

Time:11-11

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 enter image description here

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.

  • Related