python - 如何从 pymongo 运行原始 mongodb 命令

标签 python mongodb pymongo

mongo 命令行中我可以运行

db.my_collection.stats()

我需要从 Python 获取我的收藏统计信息,所以我尝试了

from pymongo import MongoClient

client = MongoClient()
db = client.test_database

collection = db.test_collection

collection.stats()

但是我明白了

TypeError: 'Collection' object is not callable. 
If you meant to call the 'stats' method on a 'Collection' object it is failing because no such method exists.

这是因为pymongo不支持该方法。如何通过 Python 将原始 mongoDB 命令发送到 mongo

最佳答案

from pymongo import MongoClient

client = MongoClient()

db = client.test_database

print(db.command("collstats", "test_collection"))

关于python - 如何从 pymongo 运行原始 mongodb 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27297727/

相关文章:

mongodb - $lookup(聚合) 使用 pymongo

python - 使用 tweepy 获取独特的推文

java - 如何从 mongodb 检索长响应?

python - Insert_one 不存在这样的方法@ pymongo 3.7.2

mongodb - mongoimport 2.6 与 2.4

mongodb - 如何使用 MongoRepository 更新 mongodb 中的几个字段?

Python 3.5 Pandas 和 MongoDB -json_normalize : raise TypeError ("data argument can' t be an iterator")

python - 从 excel 中读取专栏并进行特定的数学运算

python - 使用 python 从表存储中检索超过 1000 行

Python - 如何在测试期间取消模拟/重置模拟?