在 mongodb group/map 命令中确定组占总数的百分比 [英] In a mongodb group/map command determine the group's percentage of a total

查看:55
本文介绍了在 mongodb group/map 命令中确定组占总数的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 mongodb 组操作中,是他们在完成期间确定总数的一种方式,例如确定组占集合总数的百分比.

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 字段,该字段设置 map 可用的 Javascript 全局变量>reducefinalize 函数.您可以向 scope 添加一个字段,并在 mapreduce 阶段跟踪总数,以最适合您的用例为准;然后您可以在 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屋!

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