I have removed some fields from document definition. I want to remove this field across all documents of collection. How can I do it?
To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.
MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.
Try:
db.collection.update( { '<field>': { '$exists': true } }, // Query { '$unset': { '<field>': true } }, // Update false, // Upsert true // Multi-update )
where field
is your deprecated field and collection
is the collection it was removed from.
The general update command is of the form db.collection.update( criteria, objNew, upsert, multi )
. The false
and true
trailing arguments disable upsert mode and enable multi update so that the query updates all of the documents in the collection (not just the first match).
Update for MongoDB 2.2+
You can now provide a JSON object instead of positional arguments for upsert and multi.
db.collection.update( { '<field>': { '$exists': true } }, // Query { '$unset': { '<field>': true } }, // Update { 'multi': true } // Options )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With