【问题标题】:How I can merge sub-document (as array) on mongodb collection如何在 mongodb 集合上合并子文档(作为数组)
【发布时间】:2014-03-06 10:26:44
【问题描述】:

你好,我有一个收藏:

{
    "_id" : ObjectId("508d27069cc1ae293b36928d"),
    "title" : "This is the title",
    "body" : "This is the body text.",
    "created_date" : ISODate("2012-10-28T12:41:39.110Z"),
    "comments" : [
        {
            "subject" : "This is coment 1",
            "body" : "This is the body of comment 1.",
            "author_id" : ObjectId("508d345f9cc1ae293b369296"),
            "created_date" : ISODate("2012-10-28T13:34:23.929Z")
        },
        {
            "subject" : "This is coment 2",
            "body" : "This is the body of comment 2.",
            "author_id" : ObjectId("508d34739cc1ae293b369297"),
            "created_date" : ISODate("2012-10-28T13:34:43.192Z")
        },
        {
            "subject" : "This is coment 3",
            "body" : "This is the body of comment 3.",
            "author_id" : ObjectId("508d34839cc1ae293b369298"),
            "created_date" : ISODate("2012-10-28T13:34:59.336Z")
        }
    ]
}

所以,在仪表板的一页上,我想查看所有 cmets,我该怎么做?如何获取单个集合中每个帖子的所有 cmets,如何识别每个评论(用于编辑或删除)?

UPD1:

这是来自帖子集合的文档。我想得到这样的东西:

[
    ...,
    {
         "_generated_id_for_identify": [What the data?],
         "subject" : "This is coment 1",
         "body" : "This is the body of comment 1.",
         "author_id" : ObjectId("508d345f9cc1ae293b369296"),
         "created_date" : ISODate("2012-10-28T13:34:23.929Z")
    },
    {
         "_generated_id_for_identify": [What the data?],
         "subject" : "This is coment 2",
         "body" : "This is the body of comment 2.",
         "author_id" : ObjectId("508d34739cc1ae293b369297"),
         "created_date" : ISODate("2012-10-28T13:34:43.192Z")
    },
    ...,
    {
         "_generated_id_for_identify": [What the data?],
         "subject" : "This is coment N",
         "body" : "This is the body of comment N.",
         "author_id" : ObjectId("508d34839cc1ae293b369298"),
         "created_date" : ISODate("2012-10-28T13:34:59.336Z")
    },
    ...
]

【问题讨论】:

  • 对不起,您的问题不是很清楚。这是您的收藏现在的样子,还是您想要的样子?

标签: mongodb collections nosql-aggregation


【解决方案1】:

在帖子集合中聚合并通过“_id”匹配,然后在 cmets 和 proyection 中使用 unwind,您可以在 results 键中获得新的 cmets 集合。

db.posts.aggregate([
                   {$match:{_id:ObjectId("508d27069cc1ae293b36928d")}}, 
                   {$unwind:"$comments"}, 
                   {$project:{
                              "_id":{id:"$_id",dt:"$comments.created_date"},
                            subject:"$comments.subject",
                               body:"$comments.body",
                         "autor_id":"$comments.author_id",
                     "created_date":"$comments.created_date"}} ]).result

但是通过匹配posts._id 的简单查询,您可以获得post.cmets

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多