1

Usually I connect to a database in Pymongo like this:

    # connect to the MongoDB
    connection = MongoClient("mongodb://127.0.0.1:27017")

    # connect to the database
    self.db = self.connection.my_database

The problem is, the name of my_database always changes. How can I use a variable to choose the database?

0

1 Answer 1

4

Based on this documentation we can do it like this:

# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")

# connect to the database
a = 'my_database'
self.db = self.connection[a]
Sign up to request clarification or add additional context in comments.

2 Comments

What is "self" referring to in this example?
@NealWalters a custom class I made. Since I build it inside a class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.