如何使用mongo聚合计算不同文档的值之间的差异? [英] How to calculate difference between values of different documents using mongo aggregation?

查看:80
本文介绍了如何使用mongo聚合计算不同文档的值之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的mongo结构如下

Hi my mongo structure as below

{
"timemilliSec":1414590255,
"data":[
    {
    "x":23,
    "y":34,
    "name":"X"
    },
    {
    "x":32,
    "y":50,
    "name":"Y"
    }
    ]
},
{
"timemilliSec":1414590245,
"data":[
    {
    "x":20,
    "y":13,
    "name":"X"
    },
    {
    "x":20,
    "y":30,
    "name":"Y"
    }
    ]
}

现在我想以此方式计算第一个文档和第二个文档以及第二个到第三个文档之间的差异 因此计算如下

Now I want to calculate difference of first document and second document and second to third in this way so calculation as below

diffX = ((data.x-data.x)/(data.y-data.y)) in our case ((23-20)/(34-13))
diffY = ((data.x-data.x)/(data.y-data.y)) in our case ((32-20)/(50-30))

推荐答案

原则上这是一个棘手的问题,但是我将继续介绍您提供的两个文档的简化案例,并以此为基础制定解决方案.这些概念应该抽象化,但是对于扩展的案例则更加困难.通常可以使用聚合框架:

Tough question in principle, but I'm going to stay with the simplified case you present of two documents and base a solution around that. The concepts should abstract, but are more difficult for expanded cases. Possible with the aggregation framework in general:

db.collection.aggregate([
    // Match the documents in a pair
    { "$match": {
        "timeMilliSec": { "$in": [ 1414590255, 1414590245 ] }
    }}

    // Trivial, just keeping an order
    { "$sort": { "timeMilliSec": -1 } },

    // Unwind the arrays
    { "$unwind": "$data" },

    // Group first and last
    { "$group": {
        "_id": "$data.name",
        "firstX": { "$first": "$data.x" },
        "lastX": { "$last": "$data.x" },
        "firstY": { "$first": "$data.y" },
        "lastY": { "$last": "$data.y" }
    }},

    // Difference on the keys
    { "$project": {
        "diff": {
            "$divide": [
                { "$subtract": [ "$firstX", "$lastX" ] },
                { "$subtract": [ "$firstY", "$lastY" ] }
            ]
        }
    }},

    // Not sure you want to take it this far
    { "$group": {
        "_id": null,
        "diffX": { 
            "$min": {
                "$cond": [
                     { "$eq": [ "$_id", "X" ] },
                     "$diff",
                     false
                 ]
            }
        },
        "diffY": { 
            "$min": {
                "$cond": [
                     { "$eq": [ "$_id", "Y" ] },
                     "$diff",
                     false
                 ]
            }
        }
    }}
])

可能夸大其词,不确定其意图,但是基于示例的输出将是:

Possibly overblown, not sure of the intent, but the output of this based on the sample would be:

{ 
    "_id" : null, 
    "diffX" : 0.14285714285714285, 
    "diffY" : 0.6 
}

与计算匹配的

您可以适应您的情况,但一般原理如下所示.

You can adapt to your case, but the general principle is as shown.

最后一个流水线"阶段有一点极端",因为完成的所有操作都是将结果合并到一个文档中.否则,已经在管道中的两个文档中获得了"X"和"Y"结果.主要通过 $group 操作与 $first $last 操作以查找相应的元素在分组边界上.

The last "pipeline" stage there is a little "extreme" as all that is done is combine the results into a single document. Otherwise, the "X" and "Y" results are already obtained in two documents in the pipeline. Mostly by the $group operation with $first and $last operations to find the respective elements on the grouping boundary.

$project ,因为管道阶段执行所需的数学运算以确定不同的结果.有关更多详细信息,请参见聚合运算符,尤其是

The subsequent operations in $project as a pipeline stage performs the required math to determine the distinct results. See the aggregation operators for more details, particularly $divide and $subtract.

无论您做什么,都遵循这门课程.在您的两个键上获得一个开始"和结束"对.然后执行计算.

Whatever you do you follow this course. Get a "start" and "end" pair on your two keys. Then perform the calculations.

这篇关于如何使用mongo聚合计算不同文档的值之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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