1

The problem is :

doc1:{ 
  _id:1,
  array:['a','b','c']
},
doc2:{ 
  _id:2,
  array:['a','f','c']
}

I'm trying to remove ['a','b'] from the array field, using :

.update({_id:1},{$pull:{array:['a','b']}}) but it doesn't seem to change anything, is there a different function I should use?

Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
xShirase
  • 11,975
  • 4
  • 53
  • 85
  • possible duplicate of http://stackoverflow.com/questions/32002691/how-do-i-remove-a-string-from-an-array-in-a-mongodb-document/32002845#32002845 – Yathish Manjunath Aug 15 '15 at 13:53

1 Answers1

4

The $pull operator actually takes an argument as a "query", so the regular $in applies here:

.update({ "_id": 1 },{ "$pull": { "array": { "$in": ["a","b"] } } },{ "multi": true })

Which basically asks to $pull "either" values "a" OR "b" from the array.

Also do not forget the "multi" option when you need to update more than one document in your statement.

Blakes Seven
  • 49,422
  • 14
  • 129
  • 135
  • @xShirase Of course it does. Remember it's a "query" and also remeber to ["accept"](http://stackoverflow.com/help/accepted-answer) responses to your questions that "work". – Blakes Seven Aug 15 '15 at 12:31
  • lol you can't accept before 10minutes, and you've answerd in 2min – xShirase Aug 15 '15 at 12:35
  • @xShirase LOL indeed. Damn you stackoverflow overlords, and your cheat protection guidelines! Just kidding. But don't go to sleep or just don't forget. :) – Blakes Seven Aug 15 '15 at 12:38