MongoDB的聚合,数组值的总和 [英] MongoDB Aggregation with sum of array values

查看:947
本文介绍了MongoDB的聚合,数组值的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据的集合:

{
    "_id" : ObjectId("5516d416d0c2323619ddbca8"),
    "date" : "28/02/2015",
    "driver" : "user1",
    "passengers" : [
        {
            "user" : "user2",
            "times" : 2
        },
        {
            "user" : "user3",
            "times" : 3
        }
    ]
}
{
    "_id" : ObjectId("5516d517d0c2323619ddbca9"),
    "date" : "27/02/2015",
    "driver" : "user2",
    "passengers" : [
        {
            "user" : "user1",
            "times" : 2
        },
        {
            "user" : "user3",
            "times" : 2
        }
    ]
}

和我想执行的聚集,这样我就知道一定乘客,有时它是具有一定驱动器,在我的例子那就是:
为USER1: [{驱动程序:用户2次:2]}
,对于user2: [{驱动程序:用户1,时间:2]}
用户3: [{驱动程序:用户1,时间:3},{驱动程序:用户2次:2]}

And I would like to perform aggregation so that I will know for a certain passenger, times it was with a certain driver, in my example it would be: for user1: [{ driver: user2, times: 2}] for user2: [{ driver: user1, times: 2}] for user3: [{ driver: user1, times: 3}, {driver: user2, times:2}]

林与蒙戈很新,并知道如何使用款项进行简单的汇总,而不是在其内部的阵列,而当我的题目本身就是数组中为止。
什么是执行这种聚集适当的方式,在更具体的,我怎么在EX press.js基于服务器执行呢?

Im quite new with mongo and know how to perform easy aggregation with sum, but not when its inside arrays, and when my subject is itself in the array. what is the appropriate way to perform this kind of aggregation, and in more specific, how I perform it in express.js based server?

推荐答案

要实现与聚合框架您的需求,第一管线阶段将是一个的 $匹配对有问题的乘客,这些文件匹配 操作与乘客阵列中的用户,随后在 $放松 它解构了乘客阵列从输入文件在previous操作输出每个元素的文档操作。另一个 $匹配 的解构阵列上运行,作出进一步的过滤器previous文件流,只允许匹配文档传递未修改到下一个流水线阶段,这是投影与 必填字段 $项目 运营商。所以,本质上用户3 您汇聚管道将如:

To achieve your needs with aggregation framework, the first pipeline stage will be a $match operation on the passenger in question that matches the documents with the user in the passenger array, followed by the $unwind operation which deconstructs the passengers array from the input documents in the previous operation to output a document for each element. Another $match operation on the deconstructed array follows that further filters the previous document stream to allow only matching documents to pass unmodified into the next pipeline stage, which is projecting the required fields with the $project operator. So essentially your aggregation pipeline for user3 will be like:

db.collection.aggregate([
     {
        "$match": {
            "passengers.user": "user3"
        }
     },
     {
         "$unwind": "$passengers"
     },
     {
        "$match": {
            "passengers.user": "user3"
        }
     },
     {
         "$project": {
             "_id": 0,
            "driver": "$driver",
            "times": "$passengers.times"
        }
     }
])

结果

/* 0 */
{
    "result" : [ 
        {
            "driver" : "user1",
            "times" : 3
        }, 
        {
            "driver" : "user2",
            "times" : 2
        }
    ],
    "ok" : 1
}

更新

UPDATE:

有关使用不同的日期司机分组重复,正如你所说,你可以做一个的 $组 操作刚刚过去的 $项目之前 管线阶段,你计算使用的 $总和的总乘客次 运算符:

For grouping duplicates on drivers with different dates, as you mentioned you can do a $group operation just before the last $project pipeline stage where you compute the total passengers times using the $sum operator:

db.collection.aggregate([
     {
        "$match": {
            "passengers.user": "user3"
        }
     },
     {
         "$unwind": "$passengers"
     },
     {
        "$match": {
            "passengers.user": "user3"
        }
     },
     {
         "$group": {
             "_id": "$driver", 
             "total": {
                 "$sum": "$passengers.times"
             }
         }
     },
     {
         "$project": {
            "_id": 0,
            "driver": "$_id",
            "total": 1
        }
     }
])

结果

/* 0 */
{
    "result" : [ 
        {
            "total" : 2,
            "driver" : "user2"
        }, 
        {
            "total" : 3,
            "driver" : "user1"
        }
    ],
    "ok" : 1
}

这篇关于MongoDB的聚合,数组值的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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