[英]Time series document with mongoose node.js?
我有一个像下面这样的架构。
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
}
});
现在数据应该在文档中被推入“价值”5分钟,5分钟后而不是推入“价值”它会创建另一个文档并将数据推入“价值”接下来的5分钟等等
{"nr": "test_nr", "epoch_timestamp":"1556114198", "value":[200,300,500,200]}
5 分钟后,一个新文件,如..
{"nr": "test_nr", "epoch_timestamp":"1557114198", "value":[1200,2300,5400,1200]} and so on..
猫鼬中是否存在任何东西,或者我是否必须制定逻辑来检查时间是否少于 5 分钟,然后推入数组并每 5 分钟后一个新文档? 多谢
我唯一想到的是创建一个只有 1 个文档的新集合:
const lastEpochSchema = new mongoose.Schema({
timestamp: { type: Date },
});
module.exports = mongoose.model('LastEpoch', lastEpochSchema);
然后每次您想在activePower
插入一个新value
activePower
您只需查询:
const lastEpoch = await LastEpoch.findOne();
然后将其与当前Date.now()
进行比较,如果差异超过 5*1000(5 秒),则更新LastEpoch
并将新文档插入activePower
。 如果少于 5 秒 - 将值推送到当前activePower
,其中activePower.epoch_timestamp
将等于LastEpoch
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.