MongoDB
time intervals
data aggregation
grouping data
database tutorials

Group result by 15 minutes time interval in MongoDb

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In MongoDB, grouping results by time intervals is a common requirement for data analysis tasks, especially in scenarios such as time series data analysis, IoT data processing, and logging. MongoDB provides powerful aggregation capabilities that allow for flexible and efficient data manipulation. In this article, we'll delve into how to group results by a 15-minute time interval using MongoDB's aggregation framework.

Understanding the Aggregation Framework

MongoDB's aggregation framework facilitates data aggregation operations, allowing you to process data records and return computed results. The framework's pipeline-based approach of breaking tasks into stages enables complex data manipulations. The basic concept involves the use of stages such as $match, $group, $sort, and others to efficiently process and transform data.

Grouping by Time Intervals

When dealing with time-based data, grouping data into consistent time intervals, such as 15-minute segments, enables meaningful summarization and analysis. Suppose you have a collection of timestamped documents in your MongoDB database, you can utilize the $dateToParts and $dateFromParts operators to achieve the desired grouping.

Example: Grouping by 15-Minute Intervals

Consider a collection named sensorData with documents structured as follows:

json
1{
2  "_id": ObjectId("60c72b2f9d6c1d9f3e8f033b"),
3  "timestamp": ISODate("2023-10-09T15:31:45Z"),
4  "value": 42
5}

To group these entries into 15-minute intervals, perform the following steps using the MongoDB aggregation framework:

  1. Extract Date Components: Use the $dateToParts operator to extract components of the date such as the hour, minute, etc.
  2. Calculate 15-Minute Interval: Manipulate the minute component to represent the start of a 15-minute interval. This is done by dividing and then multiplying by 15 to align the timestamp to the nearest 15-minute mark.
  3. Recreate Date: Utilize $dateFromParts to construct a new date with modified components. The aim is to create a bucket border for the 15-minute interval.
  4. Group by Interval: Employ the $group stage to aggregate data based on this newly constructed interval date.

Here is what the aggregation pipeline might look like:

json
1db.sensorData.aggregate([
2  {
3    $addFields: {
4      parts: {
5        $dateToParts: { date: "$timestamp", timezone: "UTC" }
6      }
7    }
8  },
9  {
10    $addFields: {
11      intervalStart: {
12        $dateFromParts: {
13          'year': "$parts.year",
14          'month': "$parts.month",
15          'day': "$parts.day",
16          'hour': "$parts.hour",
17          'minute': { $multiply: [{ $floor: { $divide: ["$parts.minute", 15] } }, 15] }
18        }
19      }
20    }
21  },
22  {
23    $group: {
24      _id: "$intervalStart",
25      averageValue: { $avg: "$value" },
26      count: { $sum: 1 }
27    }
28  },
29  { $sort: { "_id": 1 } }
30])

Explanation of the Aggregation Pipeline

  • $addFields Stage:
    • parts: Extracts the date components.
    • intervalStart: Computes a new date object rounded down to the nearest 15-minute mark using $dateFromParts.
  • $group Stage:
    • Aggregates records by the computed interval start date.
    • Calculates metrics such as averageValue and count.
  • $sort Stage:
    • Sorts the results by the interval to ensure chronological order.

Key Points Summary

AspectExplanation
Date ExtractionUse $dateToParts to get components.
Interval CalculationManipulate minute with $floor and $multiply to determine the interval start.
Date ReconstructionRecreate date using $dateFromParts.
Grouping and AggregationUtilize $group to aggregate based on interval.
SortingEmploy $sort to order results.

Additional Considerations

Performance

  • Indexes: Ensure that the timestamp field is indexed to improve the performance of the aggregation.
  • Sharding: Consider sharding your collection if dealing with large datasets to distribute the load across multiple nodes.

Extensions

  • Dynamic Intervals: The interval can be parameterized to support different ranges like 30-minute or hourly intervals.
  • Multiple Metrics: Extend the aggregation to compute additional metrics like max, min, or sum values.

By leveraging MongoDB's aggregation framework with techniques to manipulate and group date components, you can efficiently extract insights from time-based data, aligning perfectly with various analytical needs.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.