【问题标题】:Get number of operations from each X minutes intervals from mongo databbase, Even non operational minutes data also从 mongo 数据库中获取每 X 分钟间隔的操作数,即使是非操作分钟数据
【发布时间】:2019-11-13 07:18:07
【问题描述】:

您好,我有一个 MongoDB 聚合问题,我想生成一个没有的报告。在数据库中完成的操作,使用聚合阶段 $match 和 $group 在匹配中提供间隔 $gte 和 $lte,在组中尝试获取每 15 分钟的数据。的操作 EVEN 0 OPERATIONS 报告也需要。

** 我用过这样的聚合**

starttime = datetime.strptime(request.args.get('start'), '%Y-%m-%d-%H-%M-%S')
    endtime = datetime.strptime(request.args.get('end'), '%Y-%m-%d-%H-%M-%S')

    reportView = []
    pipeline = [{
        "$match": {
            "time": { "$gte": starttime, "$lte": endtime}}
    },
    { "$sort": { "time": -1 } },

        { "$group": {
      "_id": {
          "$toDate": {
              "$subtract": [
                  { "$toLong": { "$toDate": "$time" }  },
                  { "$mod": [ { "$toLong": { "$toDate": "$time" } }, 1000 * 60 * 15 ] }
              ]
          }
      },
      "count": { "$sum": 1 }
    }}
    ] 

得到这个输出:

标题

[
  {
    "_id": "Sat, 12 Oct 2019 16:30:00 GMT",
    "count": 1
  },
  {
    "_id": "Tue, 22 Oct 2019 13:15:00 GMT",
    "count": 1
  },
  {
    "_id": "Fri, 01 Nov 2019 19:00:00 GMT",
    "count": 1
  },
  {
    "_id": "Thu, 31 Oct 2019 11:15:00 GMT",
    "count": 1
  }]

###### 没有得到非操作间隔##### enter code here

标题##但我需要这样的输出:

[
{
"_id": "Sat, 12 Oct 2019 16:30:00 GMT",
"count": 1
},
{
"_id": "Sat, 12 Oct 2019 16:45:00 GMT",
"count": 0
},
{
"_id": "Sat, 12 Oct 2019 17:00:00 GMT",
"count": 0
},
{
"_id": "Sat, 12 Oct 2019 17:15:00 GMT",
"count": 0
},
{
"_id": "Sat, 12 Oct 2019 17:30:00 GMT",
"count": 1
},
{
"_id": "Sat, 12 Oct 2019 17:45:00 GMT",
"count": 5
},
{
"_id": "Sat, 12 Oct 2019 18:00:00 GMT",
"count": 0
},
{
"_id": "Sat, 12 Oct 2019 18:15:00 GMT",
"count": 0
}]

【问题讨论】:

  • 请帮帮我
  • 对于 0 计数操作报告,甚至没有得到非操作间隔
  • 能否提供一些示例数据

标签: python mongodb flask aggregation-framework mongoengine


【解决方案1】:

您必须在查询之前构建您的时间范围值,因为它实际上是您查询的一个参数,而不是依赖于文档。这样做,您可以使用 $buckets 根据日期对文档进行分组。

db.collection.aggregate([
  {
    $bucket: {
      boundaries: [
        new Date("2019-11-13T00:00:00Z"),
        new Date("2019-11-13T00:15:00Z"),
        new Date("2019-11-13T00:30:00Z"),
        new Date("2019-11-13T00:45:00Z"),

      ],
      default: "outOfRange",
      groupBy: "$date",
      output: {
        count: {
          $sum: 1
        }
      }
    }
  },])

这将输出如下内容:

    [
  {
    "_id": "outOfRange",
    "count": 2
  },
  {
    "_id": ISODate("2019-11-13T00:00:00Z"),
    "count": 2
  },
  {
    "_id": ISODate("2019-11-13T00:15:00Z"),
    "count": 1
  },
  {
    "_id": ISODate("2019-11-13T00:30:00Z"),
    "count": 1
  }
]

重要提示:第​​一个输出文档 ("_id": ISODate("2019-11-13T00:00:00Z")) 将计算 ISODate("2019-11-13T00:00:00Z") $date ISODate("2019-11-13T00:15:00Z")

你可以测试the POC here

【讨论】:

  • 你的答案很好,但我需要以小时或日期或整个时间戳为单位获取边界范围,从那个时间戳间隔开始,我必须以 x 分钟为单位进行拆分
  • 我不必手动提供边界,只需获取 inerval 并在每 x 分钟内提供操作可能会发生或不重要
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-18
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
相关资源
最近更新 更多