Use mongodb aggregation framework to group by length of array

江枫思渺然 提交于 2019-12-23 08:08:58

问题


I have a collection that looks something like this:

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

etc...

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);
});

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


回答1:


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
    }
}

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.



来源:https://stackoverflow.com/questions/17955072/use-mongodb-aggregation-framework-to-group-by-length-of-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!