Cannot run a command in transaction

I am not sure if my setup is wrong or something else but I am not able to run a simple request in a transaction. I have 3 replicas with an arbiter locally. I try to do a simple find, but I keep getting an error that operation is not supported

def find_record():
    # Connect to an existing database
    mongodb_client = pymongo.MongoClient("mongodb://localhost:27019/?replicaSet=dmitriy_test")
    print("============== find_record =============")
    claim_details = mongodb_client.local.claim_details
    print(claim_details)
    with mongodb_client.start_session() as session:
        with session.start_transaction():
            print(f"+++++++++++++++++SEARCHING FOR===={pnr_id_to_search}")
            # result_from_search = claim_details.find_one({"claim_detail.assetDetails.bookingInformation.pnrId": pnr_id_to_search}, {'_id': False})
            result_from_search = claim_details.find_one({"claim_detail.assetDetails.bookingInformation.pnrId": pnr_id_to_search}, {'_id': False}, session=session)
            if(result_from_search is not None):
                print("=========================SUCCESS===============================")
            else:
                print("=========================FAILURE===============================")
            print(f"=== RESULT OF SEARCH ===")
            print(result_from_search)

this is the error that I am getting

pymongo.errors.OperationFailure: Cannot run command against the 'local' database in a transaction., full error: {'ok': 0.0, 'errmsg': "Cannot run command against the 'local' database in a transaction.", 'code': 263, 'codeName': 'OperationNotSupportedInTransaction', '$clusterTime': {'clusterTime': Timestamp(1661955990, 1), 'signature': {'hash': b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'keyId': 0}}, 'operationTime': Timestamp(1661955990, 1)}

Welcome back, @Dmitriy_Mestetskiy !

The local database is a system database intended for use by the mongod process for instance-specific data like the replication oplog.

Per the Transactions documentation, a transaction cannot read/write collections in system databases or write to system collections:

  • You cannot read/write to collections in the config , admin , or local databases.
  • You cannot write to system.* collections.

You should be able to use transactions with any non-system database (and non-system collections).

Regards,
Stennie

1 Like

THANK YOU!!! This was it!!!

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.