使用Pymongo获取关键用法
有没有办法使用pymongo
来获取键列表并计数其值?我已经使用counter
对象解决了它,并通过整个集合进行循环,但是,此代码对于广泛的集合而言效率低下。有没有办法在数据库级别上解决它?
import pymongo
from collections import Counter
client = pymongo.MongoClient("XXX")
database = client["jiripesik"]
collection = database["database"]
result = collection.find({})
key_list = []
for item in result:
for key in item.keys():
key_list.append(key)
counter = Counter(key_list)
print(counter)
Is there a way to get a list of keys and count their values using pymongo
? I have solved it using Counter
object and looping through the entire collection, however, this code would be inefficient for extensive collections. Is there a way how to solve it on the database level?
import pymongo
from collections import Counter
client = pymongo.MongoClient("XXX")
database = client["jiripesik"]
collection = database["database"]
result = collection.find({})
key_list = []
for item in result:
for key in item.keys():
key_list.append(key)
counter = Counter(key_list)
print(counter)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑这会非常表现,但是这是一种计算集合中所有顶级领域的方法。
尝试一下 mongoplayground.net.net 。
I doubt this would be very performant, but here's a way to get a count of all the top-level fields in the collection.
Try it on mongoplayground.net.