MongoDB opposite of $addToSet

MongodbDatabase

Mongodb Problem Overview


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'
  }
});

Mongodb Solutions


Solution 1 - Mongodb

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'}});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJVGView Question on Stackoverflow
Solution 1 - MongodbThiloView Answer on Stackoverflow