Question on transaction isolation

In the sample code below, what will happen if retrieved data in the transaction changes caused by a outside/separated write operation before the transaction end or committed? does transaction automatically aborted?

await session.withTransaction(async () => {
  const coll1 = client.db('mydb1').collection('foo');

  await coll1.find( { qty: { $gt: 4 } }, { session } );
  
  // some code before update
  // if the retrieved data from the above, changed due to a separate write(separate session/transaction) will this transaction abort?

  await coll1.update( 
    { id: 3 },
    { $set: { qty: 2 } },
    { session }
  )

}, transactionOptions);

I read the documentation about transaction, to my understanding it mostly offer data atomicity and durability, but I can’t find any what will happen to a transaction if any read data changed caused by outside/separate write operation

If a transaction is in progress and a write outside the transaction modifies a document that an operation in the transaction later tries to modify, the transaction aborts because of a write conflict.

If a transaction is in progress and has taken a lock to modify a document, when a write outside the transaction tries to modify the same document, the write waits until the transaction ends.

from doc.

2 Likes