Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove deprecated fields in Mongo? [closed]

Tags:

mongodb

I have removed some fields from document definition. I want to remove this field across all documents of collection. How can I do it?

like image 554
Alexey Zakharov Avatar asked Dec 27 '11 05:12

Alexey Zakharov


People also ask

How do I remove all records from a collection in MongoDB?

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.

What does .save do in MongoDB?

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.


1 Answers

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 ) 
like image 102
Tyler Brock Avatar answered Sep 20 '22 12:09

Tyler Brock