考虑以下文档:
{
"comments": [
{
"id" : 1,
"userid": "john",
"comment": "lorem ipsum...",
"responses": [
{
"id" : 2,
"userid": "smith",
"comment": "lorem ipsum...",
"responses": [
{
"id" : 3,
"userid": "random",
"comment": "lorem ipsum...",
"responses": [] // I want to push a comment into this array
},
{
"id" : 4,
"userid": "guy",
"comment": "lorem ipsum..."
}
]
},
{
"id" : 5,
"userid": "adam",
"comment": "lorem ipsum..."
}
]
}
]
}有什么方法可以将文档推入到responses数组中吗?因此,在本例中,用户想要对3级注释进行评论,而我希望将该注释推入数组中。我可以在评论时将数组位置发送给用户并返回给服务器,但我非常确定这是不可靠的。另外,如果在这两者之间会发生删除,那么(我猜(?))数组中的位置会发生变化,所以这实际上是不可行的。
发布于 2014-11-06 01:22:53
如果您知道到达该级别的索引顺序,那么是:
> db.test.update({ ... }, { "$push" : { "comments.0.responses.0.responses.0.responses" : "this is my response" } })0是索引。然而,像这样的多重嵌套结构几乎肯定是一个糟糕的数据建模选择。你应该考虑其他选择。也许modeling tree structures上的文档会有所帮助?
https://stackoverflow.com/questions/26738610
复制相似问题