Skip to content

Instantly share code, notes, and snippets.

@binarytrails
Forked from guyellis/removeMongoField.js
Created October 30, 2017 17:53
Show Gist options
  • Save binarytrails/8ad1839139af077dff70ce55aacf1368 to your computer and use it in GitHub Desktop.
Save binarytrails/8ad1839139af077dff70ce55aacf1368 to your computer and use it in GitHub Desktop.
How to remove a field from a collection in MongoDB
// Params:
// 1. query (filter) if you only want to remove this field from some records. {} will select any.
// 2. $unset the field(s) that you want to remove. Here I've set them to null but the value doesn't matter as it's ignored.
// 3. multi must be set to true otherwise it will only operate on the first record that it finds.
//
// Run this command in the MongoDB shell
db.<collectionName>.update( {}, {$unset: {<fieldName1>: null, <fieldName2>: null}}, {multi: true});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment