mongoengine + django如何计算相同内容的项目数 [英] mongoengine +django how to count the number of items of same content
问题描述
我想计算一个字段中所有不同值的数量.mongoengine中没有要使用的"annotate()",那么我该如何计算数量并按数字排序
I want to count all the numbers of different values in a field.there is no "annotate()" to use in mongoengine ,then how could i count the number and order them by numbers
我想解决的唯一方法是使用"distinct()"找出不同的值,然后使用"count()"对每个值进行计数 这是实现我想要的结果的愚蠢方式
The only way i thought out to solve this is use"distinct()"to find out the different values and then use "count()"to count each of the values it's a stupid way to realize the result i want
您还有其他方法吗?
推荐答案
MongoEngine提供了一些应满足您需求的地图简化助手. Queryset方法item_frequencies
[1]将满足您的需求.新的聚合框架没有任何特殊支持,但将来可能会添加支持.
MongoEngine has some map reduce helpers that should meet your needs. The Queryset method item_frequencies
[1] will meet your needs. There isnt any special support for the new aggregation framework but support could be added in the future.
示例用法:
BlogPost.objects.item_frequencies('tags')
这篇关于mongoengine + django如何计算相同内容的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!