使用mongodb聚合框架按数组长度分组 [英] Use mongodb aggregation framework to group by length of array

查看:419
本文介绍了使用mongodb聚合框架按数组长度分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的收藏集:

I have a collection that looks something like this:

{
    "_id": "id0",
    "name": "...",
    "saved_things": [
        { ... },
        { ... },
        { ... },
    ]
}
{
    "_id": "id1",
    "name": "...",
    "saved_things": [
        { ... },
    ]
}
{
    "_id": "id2",
    "name": "...",
    "saved_things": [
        { ... },
    ]
}

等...

我想使用mongodb的聚合框架来得出一个直方图结果,该结果可以告诉多少用户具有一定数量的saved_things.例如,对于上面的数据集,它可能返回如下内容:

I want to use mongodb's aggregation framework in order to come up with a histogram result that tells how many users have a certain count of the saved_things. For example, for the dataset above it could return something like:

{ "_id": 1, "count": 2 },
{ "_id": 3, "count": 1 }

我尝试了各种聚合函数的组合,例如下面的组合,但是没有一个能正确解决. (我感觉到我要解决这个严重的错误.)

I've tried various combinations of aggregate functions like the one below, but none have worked out correctly. (I get the feeling that I'm going about this terribly wrong.)

collection.aggregate([
    { $unwind: "$saved_things" },
    { $group: "$_id", count: { $sum: 1 } } },
    { $group: "$count", number: { $sum: 1 } } },
    { $sort: { number: -1 } }
], function(err, result) {
    console.log(result);
});

Mongo的聚合框架有可能吗?或者使用map reduce函数会更好吗?

Is this possible with Mongo's aggregate framework or would I be better off with a map reduce function?

推荐答案

好,知道了!开始了.聚合管道基本上是这样的:

Ok, got it! Here we go. The aggregation pipeline is basically that:

{
    $unwind: "$saved_things"
},
{
    $group: {
        _id: "$_id",
        size: {
            $sum: 1
        }
    }
},
{
    $group: {
        _id: "$size",
        frequency: {
            $sum: 1
        }
    }
},
{
    $project: {
        size: "$_id",
        frequency: 1,
        _id: 0
    }
}

展开saved_things数组,然后按文档_id分组并对其进行计数,这样就可以实现数组的大小.现在很容易,按size分组并计数频率.使用项目将_id字段重命名为size.

Unwind saved_things array, then group by document _id and count it, thus we can achieve the array size. Now is easy, group by size and count the frequency. Use project to rename _id field to size.

这篇关于使用mongodb聚合框架按数组长度分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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