This query does the job perfectly:
db.collection.update( { "_id": oneIdProvided }, { $inc: { "field": 5 } },{ upsert: true } )
Now, I would like to do the same operation several times with different identifiers, I thought that a good way was to use $ in, and so I tried:
db.collection.update( { "_id": { $in: oneArrayOfIds} }, { $inc: { "field": 5 } },{ upsert: true } )
The problem is this: if one of the provided identifiers in the array does not exist in the collection, a new document is created (this is what I want), but an automatic identifier will be assigned without using the identifier that I provided and was looking for.
One solution that I see is to first make a paste request with my array of identifiers (existing ones will not be changed), and then fulfill my update request with upsert: false
Do you see a way to do this in just one request?
source share