我正在使用 pymongo,但我不知道如何执行相当于“show dbs”的 mongodb 交互式 shell。
问问题
36009 次
3 回答
109
from pymongo import MongoClient
# Assuming youre running mongod on 'localhost' with port 27017
c = MongoClient('localhost',27017)
c.database_names()
2020 年更新:
弃用警告:database_names 已弃用
使用以下内容:
c.list_database_names()
于 2012-06-22T19:09:09.557 回答
12
就像今天一样
from pymongo import MongoClient
# client = MongoClient('host', port_number)
client = MongoClient('localhost', 27017)
cursor = client.list_databases()
for db in cursor:
print(db)
或者
from pymongo import MongoClient
# client = MongoClient('host', port_number)
client = MongoClient('localhost', 27017)
for db in client.list_databases():
print(db)
如果您使用 database_names,您将收到“DeprecationWarning: database_names is deprecated. Use list_database_names instead.”
于 2018-11-28T13:51:57.773 回答
5
用Python3.5,可以这样试试
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
print(client.list_database_names())
于 2019-03-02T18:03:57.390 回答