mongoengine + django如何计算相同内容的项目数 [英] mongoengine +django how to count the number of items of same content

查看:127
本文介绍了mongoengine + django如何计算相同内容的项目数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算一个字段中所有不同值的数量.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')

[1] http://docs.mongoengine.org/en/latest/apireference.html?highlight=item_frequencies#mongoengine.queryset.QuerySet.item_frequencies

这篇关于mongoengine + django如何计算相同内容的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆