'MongoDB updateMany in a single operation
I have a weird behavior with updateMany from mongoose.
Here is the main model:
const Event = {
user: {ref: 'user'},
status: String, // one of notProcessed | locked | processed
...otherDatas,
}
I have multiple instances of nodeJs app which goal is to process all the events. The only constraint here is that to keep coherence in the calculations, each instance must process events from a particular user in insertion order. Here is the simplified code of each instances:
const freeToProcessUser = await Event.findOne({ status: 'notProcessed'})
const updateResult = await Event.updateMany({
status: 'notProcessed', // this is useful in case a concurrent instance has updated the events in between the two db calls
user: freeToProcessUser._id
}, {
status: 'locked' // update status to lock so another instance cannot process it
})
if (updateInfos.modifiedCount > 0) {
// all events are now "locked"
// so here I can process events from this user
}
I use a local mongoDb instance.
The problem
The problem is that when I launch my tests, I have noticed that sometimes, 2 instances have updateInfos.modifiedCount > 0 ! For example, for 6 event in the DB for a particular user, 5 are updated by the first instance and 1 by another instance.
It happen when both instances are doing the update at the nearly same time. So it seems that both update request are modifying results...
So how can I make an updateMany update all documents in a single operation so that NO other query can run at the same time ?
Hope I have been clear enough, tell me if you need further details.
Solution 1:[1]
As the docs say:
When a single write operation (e.g. db.collection.updateMany()) modifies multiple documents, the modification of each document is atomic, but the operation as a whole is not atomic.
You need to embed the status in a single doc for each user, or make your application handle users atomically.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | qtxo |
