MongoDB-将新文档插入现有文档

MongoDB-将新文档插入现有文档,mongodb,Mongodb,我正在学习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'],

我正在学习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
   }
]
}
])

如何在现有文档中插入新注释?谢谢

使用
$push
操作符。请参阅mongo文档。

您必须使用
$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
   }}})