2014-11-24 160 views
1

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

如何在此现有文档中插入新评论?谢谢

回答

1

使用$push操作符......这不是一个真正的Stack问题 - 你应该尝试自己解决一些问题!

2

你必须使用$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 
    }}}) 
相关问题