在数组中的数组添加内容

{
        "_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"
}

得到

        "_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.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"}
                     }
                 }
              )

猜你喜欢

转载自wang-peng1.iteye.com/blog/1974608
今日推荐