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