Parth Shinojiya
Parth Shinojiya

Reputation: 11

how to rename embedded document's key field name

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

Upvotes: 1

Views: 212

Answers (1)

Kanika Singla
Kanika Singla

Reputation: 11

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

Upvotes: 0

Related Questions