Oleg
Oleg

Reputation: 2821

deleting field from document that meet some criteria

How to delete a fields from documents that meet some criteria? For exmaple, I inserted in mongodb command line documents:

db.test1.insert({LISTID: 52, 259: 180})
db.test1.insert({LISTID: 52, 259: 185})

Then I try to delete fields "259" for all documents with LISTID: 52:

db.test1.update({LISTID: 52}, {$unset: {"259" : 1}})

I looks like it delete fields "259" only for first document, but doesn't delete for the last.

Upvotes: 0

Views: 78

Answers (1)

Remon van Vliet
Remon van Vliet

Reputation: 18595

You have to set the multiple flag to true :

db.test1.update({LISTID: 52}, {$unset: {"259" : 1}}, false, true)

By default MongoDB only updates the first document that matches the criteria.

Upvotes: 4

Related Questions