Atomic write to multiple records

I am trying to update a record in CollectionA and write a record to CollectionB atomically.

The idea I am thinking is like the following:

  session.startTransaction(TRANSACTION_OPTIONS);
  collectionA.updateOne(session, Filters.and(Filters.eq(existingRecord.getId()), Filters.eq("timestamp", existingRecord.getTimestamp())), /*My Updates*/);
  collectionB.insertOne(session, /*My new record to insert*/);
  session.commitTransaction();

Consider this case: if another client is also writing to the same record in collectionA, which will update the timestamp, thus, my update operation in the above code block will do nothing (instead of throwing an exception), which will make the insertion in collectionB still succeed.
How should I prevent this, so that the update and the insert could either both succeed or both failed?

Thank you!