Assuming I have a collection in MongoDB with 5000 records, each containing something similar to:
{
"occupation":"Doctor",
"name": {
"first":"Jimmy",
"additional":"Smith"
}
Is there an easy way to rename the field “additional” to “last” in all documents? I saw the $rename operator in the documentation but I’m not really clear on how to specify a subfield.
You can use:
Or to just update the docs which contain the property:
The
false, truein the method above are:{ upsert:false, multi:true }. You need themulti:trueto update all your records.Or you can use the former way:
In MongoDB 3.2 you can also use
The general syntax of this is
https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/