如何从pymongo发出“show dbs”

时间:2012-06-22 19:02:43

标签: python mongodb pymongo

我正在使用pymongo,我无法弄清楚如何执行相当于“show dbs”的mongodb交互式shell。

3 个答案:

答案 0 :(得分:90)

from pymongo import MongoClient
# Assuming youre running mongod on 'localhost' with port 27017
c = MongoClient('localhost',27017)
c.database_names()

答案 1 :(得分:4)

今天是

corpus = ["Hello I am Sam", "This is a white desk","I ate cereals", ...]

from pymongo import MongoClient
# client = MongoClient('host', port_number)
client = MongoClient('localhost', 27017)
cursor = client.list_databases()
for db in cursor:
    print(db)

如果使用database_names,则会收到“ DeprecationWarning:不推荐使用database_names。请改用list_database_names。”

答案 2 :(得分:0)

使用Python3.5,您可以尝试这种方式

from pymongo import MongoClient
client = MongoClient('localhost', 27017)
print(client.list_database_names())