如何使用 update 命令更新 mongodb 中的文档密钥?

smi*_*ari 1 mongodb node.js mongodb-query nodejs-stream

我尝试将数据插入 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)"
}
Run Code Online (Sandbox Code Playgroud)

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

Shi*_*dey 8

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

db.collectionName.update( { _id: 1 }, { $rename: { 'brand': 'brands'} } )
Run Code Online (Sandbox Code Playgroud)

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

db.collectionName.update(
   { _id: 100 },
   { $set:
      {
        key1: value1,
        key2: value2,
        .....
      }
   }
)
Run Code Online (Sandbox Code Playgroud)