如何使用 pymongo 在 Python 中列出 MongoDB 集合名称
此示例将连接到运行在 localhost(默认端口 27017)上的 MongoDB,不带任何用户名或密码,打开名为 mydb 的数据库(另请参阅使用 pymongo 的 Python MongoDB 最小连接示例)并列出 mydb 中的所有集合名称:
mongodb_list_collections.py
from pymongo import MongoClient
client = MongoClient("mongodb://localhost")
db = client["mydb"]
print(db.list_collection_names())这将打印,例如,
mongodb_list_collections_example.py
['people', 'salaries']If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow