Search code examples
databasemongodbnosqldocument-database

How does MongoDB cursor behave when the collection is being changed?


Suppose I'm using the cursor to iterate a subset of the documents, ordered by some field let's say.

What happens if while iterating, a new document is being inserted or a current one is being deleted?

Would that affect the cursor or does it MongoDB make some sort of snapshot of the data?


Solution

  • When you are in a transaction with read concern snapshot, you are reading from a consistent snapshot of the data.

    Otherwise you could be experiencing various phenomena described here.