[英]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.