简体   繁体   English

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

[英]mongodb $lookup for nested object in array with projection

I'm having a problem using $lookup in my aggregation pipeline.我在聚合管道中使用 $lookup 时遇到问题。

I have 2 collections, members & messages我有 2 个集合、 membersmessages

members :成员:

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

messages消息

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

What I'm trying to do is,我正在努力做的是,

lookup for each recipient in : to:[{status:'Read' , recipient:ObjectId}] and populate name and email from members collection.在 : to:[{status:'Read' , recipient:ObjectId}]查找每个收件人,并从成员集合中填充姓名和电子邮件。

I tried many different things like this one;我尝试了很多不同的东西,比如这个; // //

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'
                }
            }
    ]

Many different queries including this one always return [] for members ('as': 'members').许多不同的查询,包括这个查询总是为成员返回 [] ('as': 'members')。

Just to test I tired with mongoose and .populate('threads.to.recipient','FirstName') worked perfectly.只是为了测试我厌倦了猫鼬和 .populate('threads.to.recipient','FirstName') 工作得很好。 But I cannot use mongoose for this I have to use MongoDB's native nodejs driver.但是我不能为此使用猫鼬,我必须使用 MongoDB 的本机 nodejs 驱动程序。

any advice would be greatly appreciated on this...对此的任何建议将不胜感激......

You have to use $unwind to flatten the structure of threads array before performing $lookup在执行 $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请参阅 MongoDB Playground 中的工作示例

If you don't want to use $unwind, just try the below query:如果您不想使用 $unwind,请尝试以下查询:

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

See the working example in MongoDB Playground请参阅 MongoDB Playground 中的工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM