2014-11-24 135 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 
    }}}) 
相關問題