lambda - cannot get database names from DocumentDb

0

I am trying to get a list of database names for my DocumentDB. I am using the pymongo library. I run this code;

    logging.info("get client")
    #connectionString = os.environ["documentdb_connection"]
    connectionString = "mongodb://geoff:qzmpqzmp@docdb-2023-03-22-20-49-15.cluster-cwl5gnwixa5k.us-east-1.docdb.amazonaws.com:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false"
    ##Specify the database to be used
    logging.info("Get database testdb")
    #db = client.testdb
    logging.info("All the databases")
    #db = client.testdb
    logging.info(client.list_database_names())

I have been unable to connect to the specific database I wanted so I thought I would comment those lines out and instead list all of the databases to check if it exists. On the last line of that code I get a NameError; "name 'client' is not defined" How do I fix this?

preguntada hace un año330 visualizaciones
1 Respuesta
4
Respuesta aceptada

you forgot to create the client object before attempting to use it. You should use the MongoClient class from the pymongo library to create a connection

example code:

import logging
import os
from pymongo import MongoClient

logging.info("get client")
# connectionString = os.environ["documentdb_connection"]
connectionString = "mongodb://geoff:qzmpqzmp@docdb-2023-03-22-20-49-15.cluster-cwl5gnwixa5k.us-east-1.docdb.amazonaws.com:27017/?ssl=true&ssl_ca_certs=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false"

# Create the client
client = MongoClient(connectionString)

##Specify the database to be used
logging.info("Get database testdb")
#db = client.testdb
logging.info("All the databases")
#db = client.testdb

# List all database names
logging.info(client.list_database_names())

profile picture
EXPERTO
respondido hace un año
  • Oh dear, I must have deleted that line by mistake. anyway thank you very much!

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas