mongodb - 为什么 $lookup 中的 "as"会替换整个集合?

标签 mongodb mongodb-query aggregation-framework aggregate lookup

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

集合 1:用户

> db.users.find().pretty()
{
    "_id" : ObjectId("5ee4e727d04e4b4ac1ef115b"),
    "name" : "Ashutosh Tiwari",
    "age" : 21,
    "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a9c8dac1dcddc6dac1e9cec4c8c0c587cac6c4" rel="noreferrer noopener nofollow">[email protected]</a>"
}
{
    "_id" : ObjectId("5ee4e727d04e4b4ac1ef115c"),
    "name" : "Maximilian",
    "age" : 32,
    "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bcd1ddc4fcc5ddd4d3d392dfd3d1" rel="noreferrer noopener nofollow">[email protected]</a>"
}

集合 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" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6d0c1e051819021e052d0a000c0401430e0200" rel="noreferrer noopener nofollow">[email protected]</a>"
            }
        ]
    }
}
{
    "_id" : ObjectId("5ee5353cd9f661cad505fcc8"),
    "title" : "First One",
    "author" : ObjectId("5ee4e727d04e4b4ac1ef115c"),
    "comments" : {
        "user" : [
            {
                "_id" : ObjectId("5ee4e727d04e4b4ac1ef115b"),
                "name" : "Ashutosh Tiwari",
                "age" : 21,
                "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2445574c51504b574c644349454d480a474b49" rel="noreferrer noopener nofollow">[email protected]</a>"
            }
        ]
    }
}

因此,在这里,整个评论部分现已被替换,而我希望在 comments.user 部分中获得详细信息,以便我可以看到评论和发布该评论的用户。

最佳答案

您需要先展开评论数组

您的查询可能如下所示

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

希望对你有帮助

关于mongodb - 为什么 $lookup 中的 "as"会替换整个集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62406476/

相关文章:

javascript - MongoDB - 从 Javascript 数组创建 MongooseDocumentArray

node.js - MongoDB中的changeStream和tailable游标有什么区别

mongodb - 分组文档时合并数组

mongodb - 汇总总和并查找另一个集合

mongodb - apollostack/graphql-server - 如何从解析器获取查询中请求的字段

javascript - 何时在 Mongoose 中使用 new ObjectId ("string-id")而不是 ObjectId ("string-id")?

Meteor JS 中的 MongoDB 聚合和分组问题

mongodb - Mongodb聚合排序和限制组

mongodb - 如何通过 PyMongo 和 Bottle 显示 MongoDB 数据库中的图像?

node.js - MongoDB/Mongoose 查询 : How to get different sets of data based on a field's value