Home > other >  Is it guaranteed that two update requests return distinct results?
Is it guaranteed that two update requests return distinct results?

Time:11-10

I've been looking for an answer for days without any luck. I need to know what's the expected behavior when two machines update a remote database simultaneously.

Update method

async function update () {
  const filters = { orderNumber: 1, paid: false }
  const updates = { paid: true }
  const { modifiedCount } = await OrderModel.updateOne(filters, updates)
} 

Is it guaranteed that modifiedCount will always be 0 for one machine and 1 for another?

CodePudding user response:

According to docs:

MongoDB uses locking and concurrency control to prevent clients from modifying the same data simultaneously.

And:

In addition to a shared (S) locking mode for reads and an exclusive (X) locking mode for write operations, intent shared (IS) and intent exclusive (IX) modes indicate an intent to read or write a resource using a finer granularity lock.

So exists a lock exclusive (X) for write operations.

So yes, only one updating will be doing at the same time.

Also checks What locks are taken by some common client operations?

Operation Database Collection
Update data Exclusive Exclusive
  • Related