1

I tried to insert data into mongodb.its sucessfully inserted but I facing issue wrong document insert into mongodb.how to update both key and value in document using update query in mongodb.

Json Data format

{
             "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)"
}

How to update key ex brand to brands using update query in mongodb.

Shivam Pandey
  • 3,756
  • 2
  • 19
  • 26
smith hari
  • 437
  • 1
  • 11
  • 22

1 Answers1

8

You can use $rename operator to update the key as

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

and to update the values, use $set operator,

db.collectionName.update(
   { _id: 100 },
   { $set:
      {
        key1: value1,
        key2: value2,
        .....
      }
   }
)
Shivam Pandey
  • 3,756
  • 2
  • 19
  • 26