本文介绍了如何从 pymongo 运行原始 mongodb 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mongo 命令行中,我可以运行

In a mongo command line I can run

db.my_collection.stats()

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

I need to get my collections stats from Python so I tried

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?

This is because pymongo does not support this method. How do I send raw mongoDB commands to mongo through Python?

推荐答案

from pymongo import MongoClient

client = MongoClient()

db = client.test_database

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

这篇关于如何从 pymongo 运行原始 mongodb 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 08:33