[英]Time series document with mongoose node.js?
I have a schema like this below.我有一个像下面这样的架构。
const activePowerSchema = new mongoose.Schema({
nr: {
type: String,
required: true,
minlength: 2,
maxlength: 50
},
value: {
type: [Number],
required: true,
minlength: 0
},
epoch_timestamp: {
type: Number,
required: true,
minlength: 0
}
});
now the data should be pushed into "value" for 5 mins in a document and after 5 mins instead of pushing into "value" it creates another document and push the data into "value" for next 5 mins and so on eg现在数据应该在文档中被推入“价值”5分钟,5分钟后而不是推入“价值”它会创建另一个文档并将数据推入“价值”接下来的5分钟等等
{"nr": "test_nr", "epoch_timestamp":"1556114198", "value":[200,300,500,200]}
and after 5 mins a new document like.. 5 分钟后,一个新文件,如..
{"nr": "test_nr", "epoch_timestamp":"1557114198", "value":[1200,2300,5400,1200]} and so on..
is there any thing present within mongoose or do i have to make a logic to check the time if its less then 5 min then push into array and after every 5 mins a new document?猫鼬中是否存在任何东西,或者我是否必须制定逻辑来检查时间是否少于 5 分钟,然后推入数组并每 5 分钟后一个新文档? thanks a lot多谢
The only thing that comes to my mind is creating a new collection which will have only 1 document:我唯一想到的是创建一个只有 1 个文档的新集合:
const lastEpochSchema = new mongoose.Schema({
timestamp: { type: Date },
});
module.exports = mongoose.model('LastEpoch', lastEpochSchema);
And then every time you want to insert a new value
into activePower
you simply query for:然后每次您想在activePower
插入一个新value
activePower
您只需查询:
const lastEpoch = await LastEpoch.findOne();
Then compare it with current Date.now()
and if the difference is more than 5*1000 (5 secs) then update LastEpoch
and insert a new document into activePower
.然后将其与当前Date.now()
进行比较,如果差异超过 5*1000(5 秒),则更新LastEpoch
并将新文档插入activePower
。 If less than 5 secs - push values to the current activePower
where activePower.epoch_timestamp
will be equal to LastEpoch
.如果少于 5 秒 - 将值推送到当前activePower
,其中activePower.epoch_timestamp
将等于LastEpoch
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.