user3721503
user3721503

Reputation:

Mongodb - Pull an element in an array to all documents which contains that element

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.

Upvotes: 0

Views: 172

Answers (1)

Constantin Guay
Constantin Guay

Reputation: 1654

you have to specify to update multiple documents.

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

Upvotes: 2

Related Questions