Mongodb 如何减去时间序列元素以获得与之前日期的差异?

Mongodb 如何减去时间序列元素以获得与之前日期的差异?,mongodb,mongodb-query,mongodb-atlas,Mongodb,Mongodb Query,Mongodb Atlas,我正在尝试在Mongo Atlas中构建仪表板图表。 表格应在x轴上显示日期,在y轴上显示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:

我正在尝试在Mongo Atlas中构建仪表板图表。 表格应在x轴上显示日期,在y轴上显示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仅显示“发生未知错误”


我很乐意得到一些建议:)

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

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 ] }
输出:

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

解释:我们将同一
id
count
推送到
counts
数组中,然后对其应用操作以准备一组新值,其中当前值将保留
counts
数组的当前值和以前值之间的差异。对于第一个值,前一个值被视为零。

非常感谢。这似乎起到了作用,解释对我的进一步应用有很大帮助。