$ lookup中的“ as”为什么要替换整个集合?

问题描述 投票:1回答:1

让我首先向您介绍我正在使用的2个收藏集:

集合1:用户

> db.users.find().pretty()
{
    "_id" : ObjectId("5ee4e727d04e4b4ac1ef115b"),
    "name" : "Ashutosh Tiwari",
    "age" : 21,
    "email" : "[email protected]"
}
{
    "_id" : ObjectId("5ee4e727d04e4b4ac1ef115c"),
    "name" : "Maximilian",
    "age" : 32,
    "email" : "[email protected]"
}

集合2:帖子

> db.posts.find().pretty()
{
    "_id" : ObjectId("5ee51b7ed9f661cad505fcc6"),
    "title" : "First One",
    "text" : "Hey this is the first Author",
    "author" : ObjectId("5ee4e727d04e4b4ac1ef115c"),
    "comments" : [
        {
            "user" : ObjectId("5ee4e727d04e4b4ac1ef115b"),
            "comment" : "This is my comment"
        }
    ]
}
{
    "_id" : ObjectId("5ee5353cd9f661cad505fcc8"),
    "title" : "First One",
    "author" : ObjectId("5ee4e727d04e4b4ac1ef115c"),
    "comments" : [
        {
            "user" : ObjectId("5ee4e727d04e4b4ac1ef115b"),
            "comment" : "This is my comment"
        }
    ]
}

我想让用户在第二个收藏集(帖子)中的评论数组内,由已写评论的用户替换。

我已经尝试过下面的查询,但是它正在替换注释部分!

 > db.posts.aggregate([
         { $lookup: 
                  {from: "users", 
                   localField:"comments.user",
                   foreignField:"_id",
                   as:"comments.user"
                   }   
         }  ]).pretty()

{
    "_id" : ObjectId("5ee51b7ed9f661cad505fcc6"),
    "title" : "First One",
    "text" : "Hey this is the first Author",
    "author" : ObjectId("5ee4e727d04e4b4ac1ef115c"),
    "comments" : {
        "user" : [
            {
                "_id" : ObjectId("5ee4e727d04e4b4ac1ef115b"),
                "name" : "Ashutosh Tiwari",
                "age" : 21,
                "email" : "[email protected]"
            }
        ]
    }
}
{
    "_id" : ObjectId("5ee5353cd9f661cad505fcc8"),
    "title" : "First One",
    "author" : ObjectId("5ee4e727d04e4b4ac1ef115c"),
    "comments" : {
        "user" : [
            {
                "_id" : ObjectId("5ee4e727d04e4b4ac1ef115b"),
                "name" : "Ashutosh Tiwari",
                "age" : 21,
                "email" : "[email protected]"
            }
        ]
    }
}

因此,这里,整个评论部分现在已被替换,而我想在comment.user部分中包含详细信息,这样我就可以看到该评论以及发布该评论的用户。

mongodb mongodb-query aggregation-framework aggregate lookup
1个回答
0
投票

您需要先展开评论数组

您的查询可能看起来像这样

db.posts.aggregate([
  {
    $unwind: "$comments" // unwind the comments array to get a stream of documents, each document has only one comment
  },
  {
    $lookup: {
      from: "users",
      localField: "comments.user",
      foreignField: "_id",
      as: "comments.user"
    }
  },
  {
    $unwind: "$comments.user" // we know there is only one user inside a single comment, so we can unwind this user array to be an object too (as the lookup returns an array)
  },
  {
    $group: { // then do a group by the document _id to get unique documents with comments array instead of the same document duplicated with different comments 
      _id: "$_id",
      author: {
        $first: "$author"
      },
      text: {
        $first: "$text"
      },
      title: {
        $first: "$title"
      },
      comments: {
        $push: "$comments"
      }
    }
  }
])

您可以test it here

希望有帮助

© www.soinside.com 2019 - 2024. All rights reserved.