Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it guaranteed that two update requests return distinct results?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading