Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (47.6k 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 (106k 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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...