I have a collection "user" as follows:
{
_id: "123",
"address": {
"contact1":{
"cell":"98765412345"
},
"contact2":{
"cell":"98765412346"
}
}
}
I want to rename the field 'cell' to 'mobile' in all the nested objects contact1, contact2 and so on.
The following query works:
db.user.update({},{$rename:{'address.contact1.cell':'address.contact1.mobile'}})
I would ideally want all the nested objects to be updated. So I tried
db.user.update({},{$rename:{'address.$.cell':'address.$.mobile'}})
and this doesn't work. I searched for similar issues and all of them has nested arrays instead of maps (as in my case) and the answer that I got is that $rename doesn't work on nested arrays. I would want to know if its possible using a query instead of a script
db.testuser.update({},{$rename:{'address.contact1.cell':'address.contact1.mobile'}}, false, true);
the false is for upsert:false and the true is for multi: true. You need the multi:true to update all your records. and offcourse you have to manually go through with each different key. This will update all the cell under contact1 object to mobile not the cell under contact2 object. Otherwise you have to write some scripts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With