每15分钟时间戳分割一次数据并循环遍历 [英] Split data for every 15 mins timestamp and loop through it

查看:55
本文介绍了每15分钟时间戳分割一次数据并循环遍历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据15分钟的数据执行一些计算.

I am trying to perform some calculations based on 15 minutes of data.

我一天收到这样的数据:

I get data like this for a day:

let DayData = db.sensingresults.find({
"updatedAt": {
    "$gt": ISODate("2020-06-07T00:00:00.000Z"),
    "$lte": ISODate("2020-06-08T00:00:00.000Z")
}, "vitalSensing": {"$exists": 1}
}).sort({updatedAt: 1})  

对于 DayData

我想在开始的15分钟内将数据放入任何临时数组中,并对其进行一些计算,然后将其保存到DB中.然后清空临时阵列,并在接下来的15分钟内加载数据,对其进行一些计算,然后保存到数据库中.等等...

I want to get data into any temporary array for the first 15 minutes and perform some calculations on it and save it to DB. Then empty temporary array and load with next 15 minutes data, perform some calculations on it and save to DB. and so on...

我知道如何进行计算并将其保存到数据库部分.但是我需要一些有关如何按顺序每15分钟获取数据并循环遍历的指南.

I know how to do calculations and save to DB part. But I need some guidance on how to get data for every 15mins in sequence and loop through it.

更详细的解释:我的数据总是从这样的午夜开始:2020-06-07T00:00:00.000Z.

more detailed explanation: my Data always starts from midnight : 2020-06-07T00:00:00.000Z like this.

这是我的高级建议,但不确定如何进行实施:

Here is my high level idea but not sure how to proceed with implementation:

先获取前15分钟的数据到数组中

first get data for first 15mins into an array

let tempArray = [];
tempArray.push(data for > 12.0 am to <=12.15) // after completing for loop on tempArray, come here and load next set of data. 

    for (let i = 0; i < tempArray.length; i++) {
        ... perform my calculation
        ... save to db
     }

然后将空数组tempArray = []加载到下一组> 12.15am至< = 12.30 am ....等...并执行与上述相同的操作(进行一些计算并保存到DB)

then empty array tempArray = [] and load next set of data which is >12.15am to <=12.30 am.... and so on... and perform same operations as above (do some calculations and saving to DB)

我需要在整个 DayData 上以15分钟的块的形式重复此过程.

I need to iterate this process on whole DayData in form of 15mins chunks.

推荐答案

如果我正确理解了您的问题(获取日期+ 15分钟的行),则可以将日期转换为Unix时间并添加15分钟(900000毫秒),然后将其转换回您的查询.

If I understood your question correctly (fetch row with date + 15 minutes), you could convert your date to unix time and add 15 minutes (900000 ms), then convert it back for your query.

let unixTime = Date.parse(ISOString) + 900000
let date = new Date(unixTime).toISOString()

您可以通过过滤在15分钟的间隔内输出所有对象的数组:

You can output an array of all objects in an interval of 15 minutes by filtering:

tempArray = DayData.filter(e => Date.parse(e.updatedAt) % 900000 === 0).map(e => e)

这篇关于每15分钟时间戳分割一次数据并循环遍历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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