我正在学习MongoDB的一些基础知识。我有MongoDB教程中的文档:
>db.post.insert([
{
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
title: 'NoSQL Database',
description: 'NoSQL database doesn't have tables',
by: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 20,
comments: [
{
user:'user1',
message: 'My first comment',
dateCreated: new Date(2013,11,10,2,35),
like: 0
}
]
}
])
如何在此现有文档中插入新评论?感谢
答案 0 :(得分:3)
您必须使用$push
db.coll.update({"title" : "NoSQL Database"},{$push:{"comments":{
"user":'user2',
"message": 'My second comment',
"dateCreated": new Date(2013,11,10,2,35),
"like": 0
}}})
答案 1 :(得分:1)
使用$push
运营商...这不是一个堆栈问题 - 您应该尝试解决自己!