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
{"nr": "test_nr", "epoch_timestamp":"1556114198", "value":[200,300,500,200]}
and after 5 mins a new document like..
{"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? thanks a lot
The only thing that comes to my mind is creating a new collection which will have only 1 document:
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:
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
. If less than 5 secs - push values to the current activePower
where activePower.epoch_timestamp
will be equal to LastEpoch
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.