Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB opposite of $addToSet

I'm aware of the $addToSet method for MongoDB, but I can't find a "remove" equivalent anywhere in the docs.

What's the best way to achieve this? Trying to achieve something like the following:

obj = {
  name: 'object1',
  tags: ['fus', 'ro', 'dah']
}

db.collection.update({
  name: 'object1'
}, {
  $removeFromSet: {
    tags: 'dah'
  }
});
like image 270
JVG Avatar asked Aug 23 '13 05:08

JVG


1 Answers

I think you are looking for $pull, which "removes all instances of a value from an existing array".

db.collection.update(
  {name: 'object1'}, 
  {$pull: { tags: 'dah'}});
like image 123
Thilo Avatar answered Oct 19 '22 02:10

Thilo