您好,我对 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"}
}
}
}
)
请各位我拼命寻求帮助。我想了解这一点。