Mongodb Certified Developer Associate C100dev · Free Practice Question Medium
Question 19
You're developing a service in Node.js using the MongoDB Node.js driver. You have to handle a scenario where multiple clients need to access and update certain fields of a document simultaneously. You decided to use the findAndModify method. Given the following options, which of them is the correct approach for handling the document modification?
-
A
Using the
findAndModifymethod with{ returnNewDocument: true }option. -
B
Using the
findAndModifymethod with{ new: true }option. -
C
Using the
findAndModifymethod with{ upsert: true }option. -
D
Using the
findAndModifymethod without any concurrency control options.
Reveal correct answer
Correct answer: B
A.
There is no returnNewDocument option in MongoDB's findAndModify method. This option is used in the findOneAndUpdate method.
B.
The { new: true } option ensures that the modified document is returned upon modification. This is essential in handling concurrent requests, as it allows each request to work with the most recent version of the document.
C.
The { upsert: true } option will create a new document if no documents match the query. It doesn't provide any concurrency control and could lead to data inconsistency if multiple requests are trying to update the document simultaneously.
D.
Just using the findAndModify method without any options will not guarantee the atomicity and concurrency control required in this scenario. It might lead to race conditions.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
