Go Back   |   Next Page

Bucketing Pattern

Summarizing Data

This is how we would have been storing the data...

      
{
    _id: ...,
    sensor: "Temp Sensor 456",
    values: [
        {
            ts: ISODate("2025-07-16T21:15:00.000Z"),
            value: 46
        },
        {
            ts: ISODate("2025-07-16T21:16:00.000Z"),
            value: 45
        }
    ]
}
    

It still needs us to look at every value to summarize (min, max, mean, count) the data, so let's fix that.

value_to_insert = 45;

db.readings.updateOne({
  sensor: "Temp Sensor 456",
  ts: new Date().setSecond(0, 0).setMinute(0),
}, {
  $push: {
    values: {
      ts: new Date(),
      value: value_to_insert
    }
  },
  $min: {lowest: value_to_insert},
  $max: {highest: value_to_insert},
  $sum: {count: 1},
  $sum: {total: value_to_insert}  // We use this to get the mean by dividing with count
}, {upsert: true});
    

It makes the data look like this...

      
{
    _id: ...,
    sensor: "Temp Sensor 456",
    ts: ISODate("2025-07-16T21:00:00.000Z"),  // contains all the readings for this hour
    lowest: 45,
    highest: 46,
    count: 2,
    total: 91,  // divide this with count to get mean
    values: [
        {
            ts: ISODate("2025-07-16T21:15:00.000Z"),
            value: 46
        },
        {
            ts: ISODate("2025-07-16T21:16:00.000Z"),
            value: 45
        }
    ]
}
    

This makes calculations easier because there are fewer numbers to work with - instead of having to look at 3600 numbers for an hour, there's just one - and 24 numbers for a day.

Go Back   |   Next Page