0

I'd like to atomically remove an item from an array. I was looking at using findAndModify but I'm not sure how that would work. Any direction on how to atomically remove an item from an array in a doc would be much appreciated!

Thanks!

boom
  • 10,856
  • 9
  • 43
  • 64

1 Answers1

0

You can use the $pull operator in an update or findAndModify for that:

db.collection.update({_id: id}, {$pull: {foo: 'bar'}});

This would remove the 'bar' element from the foo array field of the document matching {_id: id}.

JohnnyHK
  • 305,182
  • 66
  • 621
  • 471