I use springData and mongo 4.6. I start a transaction, update one document in one collection and then I need to check if another document in another collection exists.
@Transaction
someService() {
Object res1 = someService1DocuemntUpdate();
// It fails here.
boolean exists = anotherObjectRepository.exists(anotherObjectId);
if (exists) {
process(res1);
}
}
And I get
com.mongodb.MongoCommandException: Command failed with error 50851 (Location50851): 'Cannot run 'count' in a multi-document transaction.
Existance operation works over count operation. Count has restrictions in multi-document transactions The following doc describes workarounds https://docs.mongodb.com/manual/core/transactions/#count-operation
The easiest fix is to use findBy.. != null More smarter fix is to write agregation request.