F
F
floppa3222019-02-22 21:09:42
MongoDB
floppa322, 2019-02-22 21:09:42

Phantom reading?

Hello everyone :)
There are 2 collections: coll1 and coll2. It is necessary, based on the state of the document doc1 from coll1, to decide whether to perform an insert / update operation for coll1 and coll2.
For example:
1.Check the array length of doc1.arr1, if it satisfies the condition, then
2.Add doc2 to coll2
3.Make doc1.arr1.push(doc2.id)
It is clear that if 2 threads simultaneously check doc1.arr1. length, and the length of doc1.arr1 was one less than the maximum, then after the push, this length will take an invalid value.
The solution that came to mind was to wrap these operations in a transaction. But in the official documentation https://www.mongodb.com/blog/post/mongodb-multi-do...it says that transactional update operations will abort transactions containing update operations that attempt to update an uncommitted document. Also, transactional update-operations will lock the document for non-transactional update-operations, and those, in turn, after the lock is released, will be able to execute normally. While normal non-transactional read operations will be able to read documents, they will only not see uncommitted intermediate results of transactions. Also, as I understand, maybe I'm wrong , https://www.youtube.com/watch?time_continue=975&v=...
says that transactional read operations do not lock the document.
Question: is it possible to lock a document in a transaction for both reading and writing , so that onlytransactions could not read, it is clear that non-transactional read-operations will be available for
reading arr1.length > x, and as an update parameter, for example, increment the field allocated for this, then when trying to "check" the length of arr1, other transactions will abort.
I apologize in advance if I made a mistake in my reasoning somewhere, since I'm new to this business :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
floppa322, 2019-02-23
@Lite_stream

I managed to google the answer to the question:
Mongo uses snapshot isolation for its transactions, which guarantees all 4 levels of isolation, including the phantom reads described in the question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question