问题描述
通常,我这样连接到Pymongo中的数据库:
# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")
# connect to the database
self.db = self.connection.my_database
问题是my_database
的名称总是在变化。如何使用变量选择数据库?
解决方法
基于this documentation,我们可以这样做:
# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")
# connect to the database
a = 'my_database'
self.db = self.connection[a]