在mongodb group/map命令中,确定组占总数的百分比 [英] In a mongodb group/map command determine the group's percentage of a total
问题描述
在mongodb分组操作中,它们是在完成期间确定总数的一种方法,以便eaxample确定在集合总数中的分组百分比.
In a mongodb group operation, is their a way to determine totals during finalization, for eaxample to determine the groups percentage of the collection's total.
例如,在完成分组操作后,我知道组A的总计数量为50. 他们是否可以获取组的数量值占总数的百分比?
For example, after the grouping operation is completed, I know that group A has an aggregate qty of 50. Is their a way to get the group's qty values as a percentage of the total?
还是我必须将结果保存到集合中,确定总数,然后返回并使用计算出的组总数百分比来更新每个文档?
Or do I have to save results to a collection, determine the totals, then go back and update each document with a calculated percentage of the total for the group?
推荐答案
MongoDB map-reduce支持scope
字段,该字段将Javascript全局变量设置为可用于map
,reduce
和finalize
函数.您可以在scope
阶段添加一个字段,并在map
或reduce
阶段跟踪总数,以哪种情况最适合您的用例;您可以在finalize
期间访问它,以计算每个输出组的百分比.
MongoDB map-reduce supports a scope
field, which sets Javascript globals available to map
, reduce
, and finalize
functions. You could add a field to scope
and track the total during either map
or reduce
phases, whichever makes most sense for your use case; you can then access this during finalize
to compute the percentage for each output group.
这篇关于在mongodb group/map命令中,确定组占总数的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!