0 votes
1 view
in SQL by (47.8k 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 (107k 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 } )

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
2 answers
asked Sep 4, 2019 in SQL by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...