【问题标题】:MongoDB - insert new document into existing documentMongoDB - 将新文档插入现有文档
【发布时间】:2020-04-08 01:24:04
【问题描述】:

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

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

【问题讨论】:

    标签: mongodb


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      使用$push 运算符。请参阅 mongo 文档。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-03
        • 1970-01-01
        • 1970-01-01
        • 2020-09-08
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        • 2014-06-21
        相关资源
        最近更新 更多