在管道中针对数组进行对象值的$lookup操作

3

我有三个模型:用户物业推荐

推荐 模型包含 物业 ID留言用户 ID。我可以通过管道获取每个物业的所有推荐。

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


返回的属性看起来像这样
{
 .... other property info,
 testimonials: [
  {
    _id: '6124bbd2f8eacfa2ca662f35',
    userId: '6124bbd2f8eacfa2ca662f29',
    message: 'Amazing property',
    propertyId: '6124bbd2f8eacfa2ca662f2f',
  },
  {
    _id: '6124bbd2f8eacfa2ca662f35',
    userId: '6124bbd2f8eacfa2ca662f34',
    message: 'Worth the price',
    propertyId: '6124bbd2f8eacfa2ca662f2f',
  },
 ]
}

用户模式

firstName: {
  type: String,
  required: true,
},
lastName: {
  type: String,
  required: true,
},
email: {
  type: String,
  required: true,
},

属性模式

name: {
  type: String,
  required: true,
},
price: {
  type: Number,
  required: true,
},
location: {
  type: String,
  required: true,
},

推荐信架构

propertyId: {
  type: ObjectId,
  required: true,
},
userId: {
  type: ObjectId,
  required: true,
},
testimonial: {
  type: String,
  required: true,
},


现在的问题是如何从每个推荐中进行$lookup,以展示用户信息而非仅有id?
我希望我的响应结构如下:
{
    _id: '6124bbd2f8eacfa2ca662f34',
    name: 'Maisonette',
    price: 100000,
    testimonials: [
        {
            _id: '6124bbd2f8eacfa2ca662f35',
            userId: '6124bbd2f8eacfa2ca662f29',
            testimonial: 'Amazing property',
            propertyId: '6124bbd2f8eacfa2ca662f34',
            user: {
                _id: '6124bbd2f8eacfa2ca662f29',
                firstName: 'John',
                lastName: 'Doe',
                email: 'jd@mail.com',
            }
        },
        {
            _id: '6124bbd2f8eacfa2ca662f35',
            userId: '6124bbd2f8eacfa2ca662f27',
            testimonial: 'Worth the price',
            propertyId: '6124bbd2f8eacfa2ca662f34',
            user: {
                _id: '6124bbd2f8eacfa2ca662f27',
                firstName: 'Sam',
                lastName: 'Ben',
                email: 'sb@mail.com',
            }
        }
    ]
}

1
添加属性模式。 - ram12393
1
请在所有三个集合中展示样本文档。 - varman
1个回答

1
你可以在管道中使用$lookup阶段,
  • $lookup与用户集合一起使用
  • $addFields$arrayElemAt从上述用户查找结果中获取第一个元素
Property.aggregate([
  { $match: { _id: ObjectId(propertyId) } },
  {
    $lookup: {
      from: "propertytestimonials",
      let: { propPropertyId: "$_id" },
      pipeline: [
        {
          $match: {
            $expr: { $eq: ["$propertyId", "$$propPropertyId"] }
          }
        },
        {
          $lookup: {
            from: "users",
            localField: "userId",
            foreignField: "_id",
            as: "user"
          }
        },
        {
          $addFields: {
            user: { $arrayElemAt: ["$user", 0] }
          }
        }
      ],
      as: "testimonials"
    }
  }
])

游乐场


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接