[Python] Run mongosh query from python

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    First - disclaimer: this is my first question, be tolerant please:)

    I am building a pipeline which answers user's question in natural language about data stored in mongoDB. I am sending prompt to OpenAI API and as an answer gets a mongosh query to retrieve the relevant info from the database. That's OK. But I got stuck how to execute the query from python. What's the easiest / most natural way to do it?

    (I am new to mongoDB, I am new to running subprocesses and scripts from python and I am, let's say, intermediate with python.)

    I tried running the query with subprocess module. However, it's quite useless as it demands very strict syntax (why so?) and the LLM does not follow the format such strictly. Moreover, when I run the second and third variation from mongosh command tool, I don't get any error.

    query = "db.vendors.find({company_name:'Company123'})" # this works
    query = "db.vendors.find({company_name: 'Company123'})" # this does not (there is a space after colon)
    query = 'db.vendors.find({"company_name":"Company123"})' # neither this (there are double quotes around company_name and single qoutes around the whole string)

    connection_string = "mongodb+srv://USERNAME:pASSWORD@cluster0.x55tw.mongodb.net"
    db_name = "database"

    mongosh_command = (
    "mongosh {}/{} --quiet --eval {}".format(connection_uri, db_name, query)
    )

    result = subprocess.run(mongosh_command, shell=True, capture_output=True, text=True, check=True)
    print(json.loads(result.stdout))


    I know there is pymongo module for python. But I struggle how to pass the generated pymongo line of code to the rest of the code. The LLM returns following line of code: collection.find({"company_name":'Company123'}) and then what? Should I append it to the file with following code and then run it from another file and store results? It sounds clumsy.

    (...)
    cluster = MongoClient(connection_string)
    db = cluster["database"]
    collection = db["vendors"]


    It is not solution to ask LLM to generate only query and pass it as argument to collection.find as sometimes I want two arguments: the filter and the projection.

    What I think would be cleanest is to store the result to some mongosh script file and then run it. But the mongosh has javascript syntax so it means running javascript file from python (does it?) or whatnot and this sounds overcomplicated, too.

    So I am lost.

    Thanks you in advance for clarification!

    Continue reading...

Compartilhe esta Página