WriteConflict Error while doing BulkOperations update using Spring mongodb

Can someone help on the below issue. While doing BulkOperations update, mongoDB throws WriteConflict error.

Code:

BulkOperations bulkOperations = mongoTemplate.bulkOps(BulkOperations.BulkMode.ORDERED, CollectionConstants.COLLECTION_NAME_EQD_FIXING_AUDIT);
bulkOperations.updateOne(queryUpdates);
bulkOperations.execute();

Note: Here queryUpdates are List<Pair<Query, Update>>. In the Query object passing “_id” i.e ObjectId and all the 30K Query updates has unique ObjectId but still getting WriteConflict error. Also no other transaction is happening.

Error:

Could not commit Mongo transaction for session [ClientSessionImpl@d9d19bd id = {“id”: {“$binary”:

{“base64”: “vNL284T+Smeq2jcthOulSA==”, “subType”: “04”}

}}, causallyConsistent = true, txActive = false, txNumber = 11, error = d != java.lang.Boolean].; nested exception is com.mongodb.MongoCommandException: Command failed with error 112 (WriteConflict): ‘WiredTigerRecordStore::insertRecord :: caused by :: WriteConflict error: this operation conflicted with another operation. Please retry your operation or multi-document transaction.’ on server x01btsypdb1a:27017. The full response is {“errorLabels”: [“TransientTransactionError”], “ok”: 0.0, “errmsg”: “WiredTigerRecordStore::insertRecord :: caused by :: WriteConflict error: this operation conflicted with another operation. Please retry your operation or multi-document transaction.”, “code”: 112, “codeName”: “WriteConflict”, “$clusterTime”: {“clusterTime”: {“$timestamp”: {“t”: 1686888951, “i”: 22173}}, “signature”: {“hash”: {“$binary”: {“base64”: “sIYH92YWxYoaLCjcH8CyjZiHV08=”, “subType”: “00”}}, “keyId”: 7189425660345974791}}, “operationTime”: {“$timestamp”:

{“t”: 1686888951, “i”: 1}

}}

from the error it looks like all the 30k operations are happening inside the same transaction, and another concurrent transaction modifies something overlapping with this one, so you get an exception.

otherwise if the 30k are each a separate transaction (and their _id are all unique) you should never get a write conflict.

I’m not sure why that’s the behaviour. Maybe something related on the driver side. (perhaps that’s expected with “ordered”? no idea).

I used @Transactional. All 30K updates within single transaction. Got error for both ORDERED and UNORDERED. Is there any tool or options to monitor transactions hitting DB?

Thanks for the info. I will go through it.