【问题标题】:Inserting more properties in MongoDB document在 MongoDB 文档中插入更多属性
【发布时间】:2021-10-21 02:35:46
【问题描述】:

所以我正在研究一个 cmets,我将它们插入到现有帖子的集合中,并且我希望在我的 Mongo 文档中包含多个评论。

这是我在单个文档中设置一个属性的方式:

let result = await db.collection('posts').update({
_id : mongo.ObjectId(id)}, 
        {$set : {comment : req.body.newComment }})

那么如何在一个特定的文档中插入更多呢?

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    您可以使用push 运算符来执行此操作。

    let result = await db.collection('posts').update(
        { _id: mongo.ObjectId(id) }, 
        { $push: {comment: req.body.newComment} })
    

    MongoDB Official Documentation for push operator

    【讨论】:

      猜你喜欢
      • 2017-03-07
      • 2018-04-05
      • 1970-01-01
      • 2020-09-05
      • 2011-06-30
      • 2015-08-15
      • 2014-09-27
      • 2015-01-14
      • 1970-01-01
      相关资源
      最近更新 更多