liveBook · Manning

Tumbling window

As explained in MongoDB 8.0 in Action, Third Edition

$tumblingWindow is an Atlas Stream Processing aggregation pipeline stage that partitions a data stream into fixed, nonoverlapping time windows.

A tumbling window divides a continuous stream into consecutive, nonoverlapping intervals of a chosen duration. Each document belongs to one window, making this a good fit for hourly sales totals, daily reports, or other results that must keep periods separate. For rolling analysis that reuses events across intervals, use a hopping window instead. section 15.2.2 section 15.5

In Atlas Stream Processing, $tumblingWindow bounds an otherwise continuous stream so stateful aggregation can run on each set of documents. A stream processor can have only one window stage. Place window-dependent stages such as $group inside the window’s pipeline, then send results to a stream with $emit or write them to an Atlas database with $merge. section 15.5 section 15.6.6

To build a ten-second aggregation in mongosh:

  • Define $source and map its timestamp to the stream-processing time field. section 15.6.6
  • Define the $group aggregation, then nest it in $tumblingWindow with an interval of 10 seconds; do not also put $group in the outer processor pipeline. section 15.6.6
  • Define $merge as the Atlas database sink; create the processor with the outer stages in the order [$source, $tumblingWindow, $merge], then start it. section 15.6.6
let t = {
   $tumblingWindow: {
      interval: {
         size: NumberInt(10),
         unit: "second"
      },
      pipeline: [g]
   }
}

The window configuration sets interval.size to 10, interval.unit to "second", and pipeline to [g], where g is the separately defined $group stage. section 15.6.6

AtlasStreamProcessing>sp.mongodbInAction.sample()
{
  _id: 5,
  max_temp: 24,
  avg_watts: 48,
  median_watts: 48,
  max_watts: 53,
  min_watts: 48,
  _stream_meta: {
    source: {
      type: 'generated'
    },
    window: {
      start: ISODate('2024-09-07T15:01:30.000Z'),
      end: ISODate('2024-09-07T15:01:40.000Z')
    }
  }
}
AtlasStreamProcessing>

A sampled output document includes window start and end times, showing the ten-second interval associated with its aggregation. section 15.6.6

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains nonoverlapping membership and works through interval configuration and nested aggregation; does not detail window-closing mechanics.
Examples
Uses daily sales totals and a ten-second solar-device aggregation with sampled window start and end times.
What it assumes
Assumes familiarity with MongoDB aggregation stages while showing how to configure a stream source, window, and sink.
What you can do after
Build it from scratch in mongosh by nesting $group in a configured $tumblingWindow and writing results with $merge.
How it connects
Connects $tumblingWindow to $hoppingWindow, $group, $source, $merge, and Atlas Stream Processing aggregation pipelines.

Also explained in

Compare how 3 books explain tumbling window →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.