【问题标题】:How to update document key in mongodb using update command? [duplicate]如何使用更新命令更新 mongodb 中的文档键? [复制]
【发布时间】:2019-10-13 19:02:13
【问题描述】:

我尝试将数据插入 mongodb。它已成功插入,但我遇到了错误的文档插入 mongodb 的问题。如何使用 mongodb 中的更新查询更新文档中的键和值。

Json 数据格式

{
             "brand": "LG",
             "colour": "Black",
             "item height": "14 Millimeters",
             "item width": "14.1 Centimeters",
             "item weight": "200 g",
             "product dimensions": "13.5 x 14.1 x 1.4 cm",
             "item model number": "GP65NB60",
             "included components": "Power2Go, PowerBackup, YouCam, LabelPrint and Norton internet security (60 days trial)"
}

如何使用 mongodb 中的更新查询将关键品牌更新为品牌。

【问题讨论】:

    标签: node.js mongodb mongodb-query nodejs-stream


    【解决方案1】:

    您可以使用$rename 运算符将密钥更新为

    db.collectionName.update( { _id: 1 }, { $rename: { 'brand': 'brands'} } )
    

    要更新值,请使用$set 运算符,

    db.collectionName.update(
       { _id: 100 },
       { $set:
          {
            key1: value1,
            key2: value2,
            .....
          }
       }
    )
    

    【讨论】:

      猜你喜欢
      • 2010-12-16
      • 1970-01-01
      • 2016-08-18
      • 2018-07-21
      • 2017-01-06
      • 2016-09-28
      • 2017-03-14
      • 2017-08-15
      • 1970-01-01
      相关资源
      最近更新 更多