问题
I am using mongoengine v0.15.0. How to fetch the name of the database connected to? Of course, I would have supplied the name in the uri string. But, is there a way to query mongo and find it?
Thanks,
Harsha
回答1:
All the information about the DB connection created in the mongoengine can be found by calling get_db() which returns a pymongo.database.Database
object. Then you can access the database name in the attribute name
. Here is an example.
from mongoengine.connection import get_db, connect
connect("test_db")
# Then, somewhere where you want to get the DB name
db = get_db()
print("Database name: ", db.name)
The output:
Database name: test_db
来源:https://stackoverflow.com/questions/47833179/mongoengine-how-to-get-database-name