Skip to content

Instantly share code, notes, and snippets.

@guyellis
Last active November 30, 2021 23:36
Show Gist options
  • Save guyellis/9948091 to your computer and use it in GitHub Desktop.
Save guyellis/9948091 to your computer and use it in GitHub Desktop.
How to rename a field in all documents in a collection in MongoDB
// Params
// 1. {} add a query in here if you don't want to select all records (unlikely)
// 2. $rename one or more fields by setting the keys of the object to the old field name and their values to the new field name.
// 3. Set multi to true to force it to operate on all documents otherwise it will only operate on the first document that matches the query.
//
// Run this in the MongoDB shell.
db.<collectionName>.update( {}, { $rename: { '<oldName1>': '<newName1>', '<oldName2>': '<newName2>' } }, {multi: true} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment