Mongoengine - how to get database name?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 01:57:28

问题



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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!