0

I've a schema like this:

orders = {"productIds:[1,2]}

products = {"_id": 1}

I want to pull the value 1 (for example) in the array productIds to all orders that contains this value. I've tried with:

db.orders.update({productIds: {$elemMatch: {$eq: 1}}},{$pull: {productIds: 1}})

but this query updates only one document that has the value 1 in productIds, not all documents.

styvane
  • 59,869
  • 19
  • 150
  • 156
  • 2
    Possible duplicate of [MongoDB: How to update multiple documents with a single command?](http://stackoverflow.com/questions/1740023/mongodb-how-to-update-multiple-documents-with-a-single-command) – styvane Jan 03 '16 at 13:48

1 Answers1

2

you have to specify to update multiple documents.

db.orders.update({productIds: {$elemMatch: {$eq: 1}}},{$pull: {productIds: 1}},{multi:true})
Constantin Guay
  • 1,604
  • 2
  • 15
  • 26
  • 1
    This question has been asked and answer many many ... many times before. Please don't duplicate an answer. You shouldn't answer such question unless it provides additional details or new method to solve the problem. Instead you should flag the question as [Possible duplicate of MongoDB: How to update multiple documents with a single command?](http://stackoverflow.com/questions/1740023/mongodb-how-to-update-multiple-documents-with-a-single-command) – styvane Jan 03 '16 at 14:04
  • OP had a very specific question to $pull with arrays, which is dominantly different than generally updating multiple docs. I don't believe the commenter above read OP. – dylanh724 May 27 '18 at 15:59