如何减去时间序列元素以与之前的日期有所不同?

时间:2019-09-06 09:03:09

标签: mongodb mongodb-query mongodb-atlas

我正在尝试在Mongo-Atlas中建立仪表板图表。 该表应在x轴上显示日期,在yid上显示_id。 值应该是与之前日期的计数差。

我有一个数据点集合,例如:

_id: "someName"
timestamp: 2019-09-05T06:24:24.689+00:00
count: 50

_id: "someName"
timestamp: 2019-09-04T06:24:24.689+00:00
count: 40

...

目标是使计数差与之前的数据点相等。名字相同。

_id: "someName"
timestamp: 2019-09-05T06:24:24.689+00:00
count: 50
difference: 10

_id: "someName"
timestamp: 2019-09-04T06:24:24.689+00:00
count: 40
difference: 17

...

这样,我可以制作一张列出差异的表格

到目前为止,我已经创建了一个聚合管道

[
{$sort: {
  "timestamp": -1
}}, 
{$group: {
  _id: "$_id",
  count: {
    $push: { count: "$count", timestamp: "$timestamp" }
  }
}}, 
{$project: {
  _id: "$_id",
  count: "$count",
  countBefore: { $slice: [ "$count", 1, { $size: "$count" } ] }
}}
]

我希望减去count和countBefore,这样我就得到了一个带有数据点的数组,但有区别...

所以我尝试遵循:

{$project: {
  countDifference: {
    $map: {
      input: "$countBefore",
        as: "before",
        in: {
          $subtract: ["$$before.count", "$count.count"] 
/*"$count.count" seems to be the problem, since an integer works*/
        }
      }
    }
  }
}

Mongo Atlas仅显示“发生未知错误”

我很乐意提供一些建议:)

1 个答案:

答案 0 :(得分:0)

以下查询可以为我们提供预期的输出:

db.collection.aggregate([
    {
        $sort:{
            "timestamp":1
        }
    },
    {
        $group:{
            "_id":"$id",
            "counts":{
                $push:"$count"
            }
        }
    },
    {
        $project:{
            "differences":{
                $reduce:{
                    "input":"$counts",
                    "initialValue":{
                        "values":[],
                        "lastValue":0
                    },
                    "in":{
                        "values":{
                            $concatArrays:[
                                "$$value.values",
                                [
                                    {
                                        $subtract:["$$this","$$value.lastValue"]
                                    }
                                ]
                            ]
                        },
                        "lastValue":"$$this"
                    }
                }
            }
        }
    },
    {
        $project:{
            "_id":0,
            "id":"$_id",
            "plots":"$differences.values"
        }
    }
]).pretty()

数据集:

{
    "_id" : ObjectId("5d724550ef5e6630fde5b71e"),
    "id" : "someName",
    "timestamp" : "2019-09-05T06:24:24.689+00:00",
    "count" : 50
}
{
    "_id" : ObjectId("5d724550ef5e6630fde5b71f"),
    "id" : "someName",
    "timestamp" : "2019-09-04T06:24:24.689+00:00",
    "count" : 40
}
{
    "_id" : ObjectId("5d724796ef5e6630fde5b720"),
    "id" : "someName",
    "timestamp" : "2019-09-06T06:24:24.689+00:00",
    "count" : 61
}
{
    "_id" : ObjectId("5d724796ef5e6630fde5b721"),
    "id" : "someName",
    "timestamp" : "2019-09-07T06:24:24.689+00:00",
    "count" : 72
}
{
    "_id" : ObjectId("5d724796ef5e6630fde5b722"),
    "id" : "someName",
    "timestamp" : "2019-09-08T06:24:24.689+00:00",
    "count" : 93
}
{
    "_id" : ObjectId("5d724796ef5e6630fde5b723"),
    "id" : "someName",
    "timestamp" : "2019-09-09T06:24:24.689+00:00",
    "count" : 100
}

输出:

{ "id" : "someName", "plots" : [ 40, 10, 11, 11, 21, 7 ] }

说明::我们将同一count的{​​{1}}推入id数组中,然后对其应用$reduce操作以准备一个集合新值,其中当前值将保持counts数组的当前值与先前值之间的差。对于第一个值,先前的值将为零。