Mongo update/remove multiple fields

To remove fields from documents in a collection use:

db.test.update({data:{$exists:true}},{$unset:{data:1}},{multi:1});

Or, if you want to add new fields, use set:

db.test.update({data:{$exists:false}},{$set:{data:"your value"}},{multi:1});

Set “multi” to 1 if you want to update/remove fields in all documents that match the criteria.

2 thoughts on “Mongo update/remove multiple fields

  1. kocka's avatar kocka

    Isn’t this removing a single field from multiple documents rather than removing multiple fields from a single document?

Leave a comment