Check if mongodb database exists?

前端 未结 9 1491
孤街浪徒
孤街浪徒 2020-12-14 21:58

Is there a possibility to check if a mongo database allready exists?

9条回答
  •  再見小時候
    2020-12-14 22:51

    The PyMongo example above didn't work for me, so I rewrote it using the more standard list_databases() method to the MongoClient library:

    from pymongo import MongoClient db_name = "foo" conn = MongoClient('mongodb://localhost,localhost:27017') if bool(db_name in conn.list_databases()): print true # or return true here else: print false # or return false here

提交回复
热议问题