你好,我对mongodb相当陌生,诚实,因为我习惯于使用mysql,并且对json的熟悉程度也较低。用Mongodb将一个元素推到一个数组中的阵列
这里是从集合讨论
{
"_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"}
}
}
}
)
请大家我拼命地寻求帮助。我想了解这一点。
将查询谓词更改为“posts.post_id”:1并将$ push部分更改为“posts。$。comments”:{new comment}这是使用位置运算符:http://docs.mongodb。 org/manual/reference/operator/positions /然而,你真的不应该使用这个模式 - 你的文档增长是无限的,并且不利于性能。 – 2013-05-07 23:20:50
我已经试过了,但我会再试一次。尽管我会把它传递给我的同事,但你的意见。谢谢 – electricfeel1979 2013-05-07 23:40:08