mongodb中的聚集和15分钟前的分组聚集

问题描述 投票:0回答:1

我在mongodb中有一个架构,例如:输入

{
  _id: ObjectId("5e05c1089b3e4e333cee8c39"),
  name:"Alex",
  activity:[
        {
            _id: ObjectId("5e05c1089b3e4e333cee8c39"),
            type: 'run',
            start_timestamp: ISODate("2020-01-11T11:34:59.804Z"),
            end_timestamp: ISODate("2020-01-11T11:40:00.804Z")
        },
        {
            _id: ObjectId("5e05c1089b3e4e333cee8c40"),
            type: 'stop',
            start_timestamp: ISODate("2020-01-11T11:40:00.804Z"),
            end_timestamp: ISODate("2020-01-11T11:42:00.804Z")
        },
        {
            _id: ObjectId("5e05c1089b3e4e333cee8c41"),
            type: 'wait',
            start_timestamp: ISODate("2020-01-11T11:42:00.804Z"),
            end_timestamp: ISODate("2020-01-11T11:52:00.804Z")
        },
        {
            _id: ObjectId("5e05c1089b3e4e333cee8c41"),
            type: 'stop',
            start_timestamp: ISODate("2020-01-11T11:52:00.804Z"),
            end_timestamp: ISODate("2020-01-11T12:02:00.804Z")
        },
        {
            _id: ObjectId("5e05c1089b3e4e333cee8c41"),
            type: 'sleep',
            start_timestamp: ISODate("2020-01-11T12:02:00.804Z"),
            end_timestamp: ISODate("2020-01-11T12:48:00.804Z")
        }
  ]
}

这是一个人工活动的模式,我需要每15分钟进行一次制动(制动持续时间以分钟为单位),我已经找到了一个解决方案stackoverflow,但是这里只有一个时间戳,但在我的情况下有2个时间戳首先,我必须计算持续时间,然后根据15分钟进行分组]

输出

[
  {
        _id: "2020-01-11T11:34 to 2020-01-11T11:49" ,
        duration: "15 min",
        "brake-up":{
          run:"6 min",
          stop:"2 min",
          wait:"7 min"
        }
   },
   {
        _id: "2020-01-11T11:49 to 2020-01-11T12:04" , 
        duration: 15 min,
        "brake-up":{
          wait:"3 min"
          stop:"10 min"
          sleep:"2 min"
        }
   {
        _id: "2020-01-11T12:04 to 2020-01-11T12:19" , 
        duration: 15 min,
        "brake-up":{
          sleep:"15 min"
        }
   }
]

谢谢

node.js mongodb mongoose aggregation-framework
1个回答
1
投票
这有点

乏味

解决方案。
© www.soinside.com 2019 - 2024. All rights reserved.