1
{
    "_id" : ObjectId("5b8b8ea4b918dd57eb65a314"),
    "roll_no" : "029",
    "address" : {
        "city" : "abc",
        "street" : "xyz"
    }
}

I want to rename address->add

I tried these:

db.people.update({roll_no:"029"},{$rename:{"address.city":"add.city"}});

db.people.update({roll_no:"029"},{$rename:{"address":"add"}});
SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87

1 Answers1

0

I believe you are trying to rename the field, that can be done using:

db.people.updateMany( {}, { $rename: { "address": "add" } } )

It will change the key "address" to "add" in all the fields. However, if you just want to update one or two matching rows, you can do using:

db.people.updateMany( {_id: 1}, { $rename: { "address": "add" } } )

I hope it helps.

Thanks, Kanika

  • Thanks, But my main logic is in { "address" : "add" }. address is the field name of embedded document. I want to rename address with add – Parth Shinojiya Sep 02 '18 at 17:08