Skip to content

Instantly share code, notes, and snippets.

@wdfsinap
Forked from guyellis/renameMongoField.js
Created April 20, 2016 20:20
Show Gist options
  • Save wdfsinap/ecd5838206a54dec72abe04f81793038 to your computer and use it in GitHub Desktop.
Save wdfsinap/ecd5838206a54dec72abe04f81793038 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