Perform $lookup with value in object of array in pipeline

So I have 3 models user, property, and testimonials.

Testimonials have a propertyId, message & userId. I’ve been able to get all the testimonials for each property with a pipeline.

Property.aggregate([
  { $match: { _id: ObjectId(propertyId) } },
  {
      $lookup: {
        from: 'propertytestimonials',
        let: { propPropertyId: '$_id' },
        pipeline: [
          {
            $match: {
              $expr: {
                $and: [{ $eq: ['$propertyId', '$$propPropertyId'] }],
              },
            },
          },
        ],
        as: 'testimonials',
      },
    },
]) 

The returned property looks like this

{
 .... other property info,
 testimonials: [
  {
    _id: '6124bbd2f8eacfa2ca662f35',
    userId: '6124bbd2f8eacfa2ca662f29',
    message: 'Amazing property',
    propertyId: '6124bbd2f8eacfa2ca662f2f',
  },
  {
    _id: '6124bbd2f8eacfa2ca662f35',
    userId: '6124bbd2f8eacfa2ca662f34',
    message: 'Worth the price',
    propertyId: '6124bbd2f8eacfa2ca662f2f',
  },
 ]
}

Now the question is how do I $lookup the userId from each testimonial so as to show the user’s info and not just the id?

Hello! @Samuel_Sola-Eniafe . Welcome on board! :tada:

Could you please provide the schema of those collection? if possible some sample data would be really helpful.

Thank you.

1 Like

I’ve been able to solve this with the help of a StackOverflow user. Solution here

1 Like