简体   繁体   中英

Time-interval series database handling in node-js and Mongodb

Actually i'm creating a iot-temperature sensor database with has a event series database contain start_timestamp and end_timestamp, for every temperature change event i inserting a new document in database, My Schema is

var tempSchema = mongoose.Schema({
    sensor_name:{
        type:String
    },
    temp_value:{
        type:Number
    },
    start_timestamp:{
        type:Date,
    },
    end_timestamp:{
        type:Date,
        default:null
    }
});

for every new document insert i have to first update end_timestamp of last event and then insert new document with same timestamp which i updated last document end_timestamp.

Problems i'm facing

1) i have 30 sensors and every 2 sec i'm getting data from sensor and first i'm comparing current value with last value and if this value change i'm firing a event and update last value which has end_timestamp null,because of event loop is very busy and async nature of node sometime 2 entry of same event inserting on database the only difference start_timestamp is different (like 2020-02-22T15:52:21.639+00:00 and 2020-02-22T15:52:21.710+00:00) but ideally should be only one end_timestamp null in database

2) 2 operations is happening on same time, right now i'm using 2 function for separate opration like update_entry for end_timestamp update and post_entry for new document,is there is any method in mongodb i used to perform this operation together.

Give me your kind help Thanks in advance

My approach would be to create two collections (or one collection and an aggregation).

The first collection contains only the raw sensor data, I assume they have just the timestamp value:

db.collection.insertMany([
   { sensor_name: "sensor 1", temp_value: 30.2, timestamp: new Date("2020-02-22T15:52:21.639Z") },
   { sensor_name: "sensor 1", temp_value: 31.2, timestamp: new Date("2020-02-22T15:52:23.639Z") },
   { sensor_name: "sensor 1", temp_value: 32.2, timestamp: new Date("2020-02-22T15:52:25.639Z") },
   { sensor_name: "sensor 1", temp_value: 32.2, timestamp: new Date("2020-02-22T15:52:27.639Z") },
   { sensor_name: "sensor 1", temp_value: 32.2, timestamp: new Date("2020-02-22T15:52:29.639Z") },
   { sensor_name: "sensor 3", temp_value: 12.2, timestamp: new Date("2020-02-22T15:52:00.639Z") }
])

Then you can aggregate the values:

db.collection.aggregate([
   { $sort: { timestamp: 1 } },
   {
      $group: {
         _id: "$sensor_name",
         temp_value: { $last: "$temp_value" },
         start_timestamp: { $first: "$timestamp" },
         count: { $sum: 1 },
         end_timestamp: { $last: "$timestamp" },
         min_temp: { $min: "$temp_value" },
         max_temp: { $max: "$temp_value" },
      }
   }
])

If you need to store the result in a collection, append { $merge: { into: "temperatures" } } to the aggregation pipeline.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM