Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo DB Delete a field and value

Tags:

mongodb

In mongo shell how would I delete all occurrences of "id" : "1" the value of the field is always different. Would I use the $unset operator? Would that delete the value and the field?

like image 750
Jacinto Avatar asked May 13 '12 23:05

Jacinto


1 Answers

You're saying remove all occurrences of the field, right? If so, then it should be like this:

db.collection.update( 
    { id: { $exists: true } },  // criteria
    { $unset: { id: 1 } },      // modifier
    false,                      // no need to upsert
    true                        // multi-update
);
like image 199
McGarnagle Avatar answered Nov 15 '22 07:11

McGarnagle