0

您好,我对 mongodb 还很陌生,老实说,因为我习惯了 mysql,而且对 json 的了解也较少,所以老实说这仍然让我感到困惑。

这是我收藏的文档 讨论

{
        "_id" : ObjectId("5188c93f0361ca6dc33e3a30"),
        "admin" : [ ],
        "created" : "2013-04-30 19:10:21",
        "description" : "guitar theory",
        "members" : [ ],
        "modified" : "2013-04-30 19:10:21",
        "name" : "Arpeggios",
        "posts" : [
                {
                        "post_id" : "1",
                        "user_id" : "1",
                        "name" : "Test",
                        "slug" : "xxx",
                        "comment" : "xxx",
                        "created" : "xxx",
                        "modified" : "xxx",
                        "comments" : [ ],
                        "attachments" : [ ]
                },
                {
                        "post_id" : "2",
                        "user_id" : "1",
                        "name" : "Test",
                        "slug" : "xxx",
                        "comment" : "xxx",
                        "created" : "xxx",
                        "modified" : "xxx",
                        "comments" : [ ],
                        "attachments" : [ ]
                }
        ],
        "profile_pic" : "adasdad",
        "settings" : [ ],
        "slug" : "arpeggio"
}

我的目标是在 post_id = 1 的数组评论上推送一个元素,以获取图片这是我想要的结果:

{
        "_id" : ObjectId("5188c93f0361ca6dc33e3a30"),
        "admin" : [ ],
        "created" : "2013-04-30 19:10:21",
        "description" : "guitar theory",
        "members" : [ ],
        "modified" : "2013-04-30 19:10:21",
        "name" : "Arpeggios",
        "posts" : [
                {
                        "post_id" : "1",
                        "user_id" : "1",
                        "name" : "Test",
                        "slug" : "xxx",
                        "comment" : "xxx",
                        "created" : "xxx",
                        "modified" : "xxx",
                        "comments" : [ 
                        {"comment_id":"xxx", "user_id":"xxx", "name":"xxx","comment":"xxx", "created":"xxx", "modified":"xxx"},
                        {"comment_id":"xxx", "user_id":"xxx", "name":"xxx","comment":"xxx", "created":"xxx", "modified":"xxx"}
                        ],
                        "attachments" : [ ]
                },
                {
                        "post_id" : "2",
                        "user_id" : "1",
                        "name" : "Test",
                        "slug" : "xxx",
                        "comment" : "xxx",
                        "created" : "xxx",
                        "modified" : "xxx",
                        "comments" : [ ],
                        "attachments" : [ ]
                }
        ],
        "profile_pic" : "adasdad",
        "settings" : [ ],
        "slug" : "arpeggio"
} 

我已经彻底研究了几个小时,这就是我想出的,这只是一个失败并且不起作用:

db.discussions.update(
                {_id:ObjectId("5188c93f0361ca6dc33e3a30"), posts:{post_id:1}}, 
                {$push:
                    {"posts:
                        {comments:
                            {"comment_id":"xxx", "user_id":"xxx", "name":"xxx","comment":"xxx", "created":"xxx", "modified":"xxx"}
                        }
                    } 
                }
                )

请各位我拼命寻求帮助。我想了解这一点。

4

1 回答 1

1

有几件事正在发生。

首先,您要使用$ 位置运算符

其次,您提到您的集合名称是 Discussion 但您的更新使用“讨论” - 确保您使用与实际集合名称相同的名称。

第三,您在列出的文档中的 post_id 是“1”,但您尝试匹配 1。字符串“1”将不等于数字 1。请注意您的类型。

这种语法(注意我也纠正了不平衡的引号)应该这样做:

db.discussion.update(
             {_id:ObjectId("5188c93f0361ca6dc33e3a30"), "posts.post_id":"1"},
             {$push:
                  {"posts.$.comments":
                         {"comment_id":"xxx", "user_id":"xxx",
                          "name":"xxx","comment":"xxx", "created":"xxx",
                           "modified":"xxx"}
                     }
                 }
              )

我要重申我不喜欢这种模式——你没有限制你的文档增长,这会导致性能问题,更不用说让它比你需要的更复杂(而且更大)。

于 2013-05-08T00:01:03.020 回答