Python Pymongo中如何使用变量选择数据库
通常我连接到Pymongo中的数据库,如下所示:Python Pymongo中如何使用变量选择数据库,python,database,mongodb,database-connection,pymongo,Python,Database,Mongodb,Database Connection,Pymongo,通常我连接到Pymongo中的数据库,如下所示: # connect to the MongoDB connection = MongoClient("mongodb://127.0.0.1:27017") # connect to the database self.db = self.connection.my_database # connect to the MongoDB connection = MongoClient(&quo
# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")
# connect to the database
self.db = self.connection.my_database
# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")
# connect to the database
a = 'my_database'
self.db = self.connection[a]
问题是,我的_数据库的名称总是更改。如何使用变量选择数据库?基于此,我们可以这样做:
# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")
# connect to the database
self.db = self.connection.my_database
# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")
# connect to the database
a = 'my_database'
self.db = self.connection[a]
基于此,我们可以这样做:
# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")
# connect to the database
self.db = self.connection.my_database
# connect to the MongoDB
connection = MongoClient("mongodb://127.0.0.1:27017")
# connect to the database
a = 'my_database'
self.db = self.connection[a]
本例中的自我指代是什么?本例中的自我指代是什么?