pythonmongodbjdbcjaydebeapi

Error Connecting MongoDB with JDBC: CommandNotFound for 'sqlGetResultSchema'


I am using MongoDB docker image mongo:4.4 and is working fine with DBeaverEE (MongoDB) and Robo 3T.

I am trying to connect with MongoDB with JDBC using jaydebeapi.connect(...), also I have put all related jars (such as mongodb-jdbc-2.1.4.jar and mongodb-driver-sync-4.7.1.jar).

My Code: I added {username}:{password} in a string as it gave me a null username error without it.

driver_name = "com.mongodb.jdbc.MongoDriver"
jdbc_url = f"jdbc:mongodb://{username}:{password}@{host}:{port}/{database}?authSource=admin&authMechanism=SCRAM-SHA-1"

jar_folder = "jars_test"

jar_folder = os.path.abspath(jar_folder)

jar_files = [os.path.join(jar_folder, file) for file in os.listdir(jar_folder) if file.endswith(".jar")]

# Start the JVM with the JDBC driver JAR files
if not jpype.isJVMStarted():
    # Start the JVM with the JDBC driver JAR files
    jpype.startJVM(
        jpype.getDefaultJVMPath(),
        "-Djava.class.path={}".format(os.pathsep.join(jar_files))
    )

try:
    # Connect to MongoDB using JayDeBeApi
    conn = jaydebeapi.connect(
        driver_name,
        jdbc_url,
        [username, password],
    )
    print("Connection successful and query executed.")
    conn.close()

except Exception as e:
    print(f"Error: {e}")
    raise e

And I got this error:

com.mongodb.MongoCommandException: com.mongodb.MongoCommandException: Command failed with error 59 (CommandNotFound): 'no such command: 'sqlGetResultSchema'' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "no such command: 'sqlGetResultSchema'", "code": 59, "codeName": "CommandNotFound"}

I didn't find any solution for it and the assumption is that Mongo DB doesn't support this Command.

Any Suggestions and possible solutions for MongoDB connectivity using JDBC?


Solution

  • Seems like you are using Atlas JDBC driver. I ran into the same issue earlier today with this driver specifically.

    For the moment being, try using a different JDBC driver, like https://github.com/wise-coders/mongodb-jdbc-driver - it worked for me