如何在同一个MongoDB聚合$ group查询中计算多个键? [英] How do I count multiple keys in the same MongoDB aggregation $group query?

查看:196
本文介绍了如何在同一个MongoDB聚合$ group查询中计算多个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询:

db.test.aggregate( {$group : { _id : '$key', frequency: { $sum : 1 } } } )

这将获得测试集中每个键枚举的频率.基本上,我已经分配了密钥.

This will get the frequency of every enumeration of key in the test set. Basically, I have gotten the distribution of key.

现在想像一下,我想获取key1,key2和key3的分布(所以有三个不同的分布).

Now imagine I want to get the distributions of key1, key2, and key3 (so three different distributions).

很明显,我可以使用每个单独的键运行此查询3次,但似乎我们可以通过允许它同时计算所有3个键来优化查询.我一直在研究它并搜索整个网络,但是到目前为止,我被委托运行三个单独的聚合查询或使用map/reduce函数.

Obviously, I could run this query 3 times with each separate key, but it seems like we would be able to optimize the query by allowing it to count all 3 keys at the same time. I have been playing around with it and searching the whole of the inter-webs, but so far, I am consigned to running three separate aggregation queries or using a map/reduce function.

还有其他想法吗?

推荐答案

您可以在此处使用几种不同的方法:

There are a few different approaches you could use here:

  1. 使用map/reduce:请勿执行此操作.现在,在此用例中,运行聚合框架3次比使用map reduce函数要快得多.

  1. Use map/reduce: don't do this. Right now it would be much faster to run the aggregation framework 3 times than to use a map reduce function for this use case.

运行聚合3次.这不是最佳选择,但是如果您没有时间限制,那么这是最简单的选择.如果您的汇总采用<无论如何,只要几秒钟,我就不会担心优化,直到它们成为问题为止.

Run aggregation 3 times. This is not optimal, but if you don't have time constraints then this is the easiest option. If your aggregations are taking < a few seconds anyway then I wouldn't worry about optimizing until they become a problem.

这是我能想到的最佳解决方法. $group运算符允许您在多个字段上构建_id.例如. {"_id":{"a":"$key1", "b":"$key2", "c":"$key3"}}.这样做会为不同键的所有现有组合创建一个分组.您可能会以这种方式对键进行分组,然后在客户端中手动汇总结果.

Here's the best work-around I can think of. The $group operator allows you to build an _id on multiple fields. E.g. {"_id":{"a":"$key1", "b":"$key2", "c":"$key3"}}. Doing this creates a grouping for all existing combinations of your different keys. You could potentially group you keys this way and then manually sum across the results in the client.

让我详细说明.假设我们有形状的集合.这些形状可以具有颜色,大小和种类(正方形,圆形等).多键ID上的汇总可能如下所示:

Let me elaborate. Let's say we have a collection of shapes. These shapes can have a color, a size, and a kind (square, circle, etc). An aggregation on a multi-key Id could look like:

db.shapes.aggregate({$group:{_id:{"f1":"$f1", "f2":"$f2", "f3":"$f3"}, count:{"$sum":1}}})

并返回:

"result" : [
        {
            "_id" : {
                "f1" : "yellow",
                "f2" : "medium",
                "f3" : "triangle"
            },
            "count" : 4086
        },
        {
            "_id" : {
                "f1" : "red",
                "f2" : "small",
                "f3" : "triangle"
            },
            "count" : 4138
        },
        {
            "_id" : {
                "f1" : "red",
                "f2" : "big",
                "f3" : "square"
            },
            "count" : 4113
        },
        {
            "_id" : {
                "f1" : "yellow",
                "f2" : "small",
                "f3" : "triangle"
            },
            "count" : 4145
        },
        {
            "_id" : {
                "f1" : "red",
                "f2" : "small",
                "f3" : "square"
            },
            "count" : 4062
        }

...等等

然后,您将在客户端上大幅减少条目的数量,从而对结果进行总结.假设每个键的唯一值数量与文档总数相比足够少,那么您可以在很短的时间内完成最后一步.

You would then sum up the results client-side, over a drastically reduced number of entries. Assuming the number of unique values for each key is sufficiently small compared to the total number of documents, you could do this final step in a negligible amount of time.

这篇关于如何在同一个MongoDB聚合$ group查询中计算多个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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