微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mongodb $ lookup用于投影数组中的嵌套对象

如何解决mongodb $ lookup用于投影数组中的嵌套对象

在聚合管道中使用$ lookup时遇到问题。

我有2个收藏集,membersmessages

成员:

{_id,FirstName,LastName,Email,...}

消息

{
  _id:ObjectId('xxx'),createdBy:ObjectId(''),...
  threads:[
    {  message:'',attachments:[],from:ObjectId,to:[{status:'Read',recipient:ObjectId}] }]
}

我想做的是

to:[{status:'Read',recipient:ObjectId}]中查找每个收件人,并填充成员集合中的姓名和电子邮件

我尝试了很多类似的事情。 //

db.messages.aggregate([
     {
                '$lookup': {
                    'from': 'members','let': {
                        'memberId': '$threads.to.recipient'
                    },'pipeline': [
                        {
                            '$match': {
                                '$expr': {
                                    '$eq': [
                                        '$$memberId','$members._id'
                                    ]
                                }
                            }
                        },{$project: {FirstName: 1,_id: 1,LastName: 1,Email: 1}}
                    ],'as': 'members'
                }
            }
    ]

包括查询在内的许多不同查询总是返回成员的[]('as':'members')。

只是测试一下,我对猫鼬和.populate('threads.to.recipient','FirstName')的工作非常满意。但是我不能使用猫鼬,而必须使用MongoDB的本地nodejs驱动程序。

对此,任何建议将不胜感激。

解决方法

在执行$ lookup之前,必须使用$unwind来展平threads数组的结构

db.messages.aggregate([
  {
    $unwind: "$threads"
  },{
    $unwind: "$threads.to"
  },{
    $lookup: {
      from: "members",let: {
        memberId: "$threads.to.recipient"
      },as: "members",pipeline: [
        {
          $match: {
            $expr: {
              $eq: [
                "$$memberId","$_id"
              ]
            }
          }
        },{
          $project: {
            FirstName: 1,_id: 1,LastName: 1,Email: 1
          }
        }
      ]
    }
  }
])

See the working example in MongoDB Playground

如果您不想使用$ unwind,只需尝试以下查询:

db.messages.aggregate([
  {
    "$lookup": {
      "from": "members","localField": "threads.to.recipient","foreignField": "_id","as": "members"
    }
  }
])

See the working example in MongoDB Playground

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。