按时间窗口之间的时间间隔分组 [英] Group by time interval between window of time

查看:263
本文介绍了按时间窗口之间的时间间隔分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下文档结构:

{  
   "_id":"5c59c35d8610f702d00e6f70",
   "ipAddress":"50.116.14.48",
   "startTime":"2018-02-06T12:01:59.000Z",
   "endTime":"2018-02-06T12:31:00.000Z", 
}

我希望能够在15分钟的时间范围内对事件进行分组.例如;鉴于以上文档的结构,我认为该文档将在0-15分钟,15-30分钟和30-45分钟内都算作一次事件.

I would like to be able to group occurrences within a 15 min window of time. For example; Given the structure of the document above I would assume this document would count as an occurrence for both 0-15mins, 15-30mins, and 30-45mins.

结果看起来像这样:

[  
   {  
      "occurrences":1,
      "startWindow":"2018-02-06T12:00:00.000Z",
      "endWindow":"2018-02-06T12:15:00.000Z"
   },
   {  
      "occurrences":1,
      "startWindow":"2018-02-06T12:15:01.000Z",
      "endWindow":"2018-02-06T12:30:00.000Z"
   },
   {  
      "occurrences":1,
      "startWindow":"2018-02-06T12:30:01.000Z",
      "endWindow":"2018-02-06T12:45:00.000Z"
   }
]

我看过很多例子,它们仅按一个时间间隔对一个日期进行分组,但是这种情况下文档有时间窗呢?

I have seen many examples that group only by a single date on an interval, but how about this situation where the document has a window of time?

您如何建立此聚合?

推荐答案

除了mickl的时间数学之外,您还需要使用

In addition to mickl's time math you need to use $range to "spread" the document across all "windows" between start and end:

db.col.aggregate([
    { $addFields: {
        // an array of 15 min intervals between startTime and endTime
        window: { $range: [ 
            { $floor: { $divide: [ { $toLong: { $toDate: "$startTime" } }, 900000 ] }  }, 
            { $ceil: { $divide: [ { $toLong: { $toDate: "$endTime" } }, 900000 ] }  }
        ] }
    } },
    // 1 document per interval
    { $unwind: "$window" },
    // group by interval
    { $group: {
        _id: "$window",
        occurrences: { $sum: 1 }
    }},
    // to match expected order
    {$sort: {_id:1}},
    // calculate window boundaries
    { $project: {
        _id: 0,
        occurrences: 1,
        startWindow: { $toDate: { $add: [ { $multiply: [ "$_id", 900000 ] }, 1000 ] } },
        endWindow: { $toDate: { $multiply: [ { $add: [ "$_id", 1 ] }, 900000 ] } }        
    } }
])

这篇关于按时间窗口之间的时间间隔分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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