0 votes
1 view
in SQL by (32.5k points)

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.

1 Answer

0 votes
by (76.8k points)

To rename a field for all documents in MongoDB you should try the below-mentioned code:-

 db.collectionName.update({}, { $rename : { 'name.additional' : 'name.last' } }, { multi: true } )

...