如何在MongoDB上将数据更新为多层结构
我想将一个新的注释文档嵌入到它的父注释中 这是一篇关于MongoDB的博客文章的文档如何在MongoDB上将数据更新为多层结构,mongodb,Mongodb,我想将一个新的注释文档嵌入到它的父注释中 这是一篇关于MongoDB的博客文章的文档 { '_id': 1, 'title': xxx, 'content': xxx, 'comments': [ { 'id': 1, 'author': xxx, 'content':xxx }, { 'id':
{ '_id': 1,
'title': xxx,
'content': xxx,
'comments': [
{
'id': 1,
'author': xxx,
'content':xxx
},
{
'id': 2,
'author': xxx,
'content':xxx
}
]
}
我想添加一个新的注释,它的父级注释id为1
我怎样才能做到以下几点:
'comments': [
{
'id': 1,
'author': xxx,
'content':xxx
'comments':[
{
'id':3,
'author': xxx,
'content':xxx
}
]
},
{
'id': 2,
'author': xxx,
'content':xxx
}
]
谢谢 您可以按如下方式执行:
db.collection.update({"_id" : 1, "comments.id" : 1},{$push : {"comments.$.comments" : {id : 3, author : "xxx", content:"xxx"}}})
我对“评论.$.comments”有点困惑。“$”的作用是什么?@user2742381:the
$
是最重要的。它充当查询中每个文档的第一个匹配数组元素的占位符(因此在本例中,comments.id
所在数组的第一个元素是1
)。@Stennie我明白了。谢谢您。