Mongoengine - how to get database name?

前端 未结 1 820
温柔的废话
温柔的废话 2021-01-15 03:52


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

1条回答
  •  有刺的猬
    2021-01-15 04:21

    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
    

    0 讨论(0)
提交回复
热议问题