MongoEngine-如何获取数据库名称? [英] Mongoengine - how to get database name?

查看:236
本文介绍了MongoEngine-如何获取数据库名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用mongoengine v0.15.0.如何获取连接的数据库的名称?当然,我会在uri字符串中提供名称.但是,有没有办法查询mongo并找到它?


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

推荐答案

可以通过调用get_db()来找到有关在mongoengine中创建的数据库连接的所有信息,该方法返回一个pymongo.database.Database对象.然后,您可以在属性name中访问数据库名称.这是一个例子.

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)

输出:

Database name: test_db

这篇关于MongoEngine-如何获取数据库名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆