【问题标题】:How to calculate difference between values of different documents using mongo aggregation?如何使用 mongo 聚合计算不同文档的值之间的差异?
【发布时间】:2014-10-29 13:55:02
【问题描述】:

你好我的mongo结构如下

{
"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"
    }
    ]
}

现在我想以这种方式计算第一个文档和第二个文档以及第二个到第三个文档的差异 所以计算如下

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

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:

    原则上是个棘手的问题,但我将继续使用您提供的两个文档的简化案例,并以此为基础制定解决方案。这些概念应该是抽象的,但对于扩展的情况来说更难。通常可以使用aggregation framework

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

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

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

    与计算匹配。

    您可以根据自己的情况进行调整,但一般原则如图所示。

    最后一个“管道”阶段有点“极端”,因为所做的只是将结果合并到一个文档中。否则,“X”和“Y”结果已经在管道中的两个文档中获得。主要是通过$group 操作与$first$last 操作来查找分组边界上的相应元素。

    $project 中的后续操作作为流水线阶段执行所需的数学运算以确定不同的结果。请参阅aggregation operators 了解更多详情,尤其是$divide$subtract

    无论你做什么,你都遵循这个课程。在您的两个键上获取“开始”和“结束”对。然后进行计算。

    【讨论】:

      猜你喜欢
      • 2014-10-27
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 2018-07-25
      相关资源
      最近更新 更多