Can lock yielding break query isolation?

157 views Asked by At

Mongo docs talk about queries yielding locks to avoid blocking other operations. Will Mongo yield the lock from a read to a write that changes the read result?

Say I've got docs {x:1}, {x:2}, {x:2}, {x:1} and I'm reading find({x:2}). Assume the fourth doc isn't in the working set, so Mongo page faults, yielding the lock to an update({x:1}, {x:2}, {multi: true}), which completes and returns the lock to the find. The find would now include the fourth doc but omit the first doc. Does Monogo work like this?

1

There are 1 answers

0
Asya Kamsky On

In MongoDB there is no guarantee of query isolation - in fact, across multiple documents, you are not guaranteed to be looking at the same point in time.

What you describe can absolutely happen, and it does. The same is true of multi-document queries that fetch a large number of documents in batches (when you use a cursor). When you issue getmore for the next batch, the state of the data is not guaranteed to be the same as when you got the previous batch of results.