Time series data is a type of data collected at regular intervals over time. Common examples include stock prices, temperature readings, and website traffic. MongoDB is well-suited for managing time series data due to its flexibility, scalability, and ability to handle large datasets. In this chapter, we’ll cover everything from basic time series data concepts to advanced techniques for storing, managing, and analyzing time series data in MongoDB.
Time series data is simply a sequence of data points recorded at successive times, typically with equal time intervals between them. Examples include:
mongod
and start the MongoDB shell with mongo
.MongoDB provides a specialized schema for time series collections, optimized to handle large volumes of time-stamped data.
db.createCollection("temperatureReadings", {
timeseries: {
timeField: "timestamp", // Field for the time
metaField: "location", // Field for metadata (e.g., location, device ID)
granularity: "minutes" // Data collection interval (seconds, minutes, or hours)
}
});
Once the collection is created, you can insert time series data as regular documents.
db.temperatureReadings.insertMany([
{ timestamp: new Date("2024-10-25T08:00:00Z"), location: "New York", temperature: 18.2 },
{ timestamp: new Date("2024-10-25T09:00:00Z"), location: "New York", temperature: 18.5 },
{ timestamp: new Date("2024-10-25T10:00:00Z"), location: "New York", temperature: 19.1 }
]);
Each document contains:
MongoDB provides several query options to filter and aggregate time series data.
Retrieve data based on time ranges:
db.temperatureReadings.find({
timestamp: { $gte: new Date("2024-10-25T08:00:00Z"), $lte: new Date("2024-10-25T10:00:00Z") }
});
MongoDB’s aggregation framework enables you to analyze time series data using various stages such as $match
, $group
, and $sort
.
db.temperatureReadings.aggregate([
{
$group: {
_id: { hour: { $hour: "$timestamp" } },
avgTemperature: { $avg: "$temperature" }
}
},
{ $sort: { "_id.hour": 1 } }
]);
MongoDB allows for advanced time series analysis using aggregation pipelines and additional operators.
Identify trends over time by calculating the moving average of temperature data.
db.temperatureReadings.aggregate([
{ $sort: { timestamp: 1 } },
{
$setWindowFields: {
partitionBy: "$location",
sortBy: { timestamp: 1 },
output: {
movingAvgTemp: { $avg: "$temperature", window: { range: [-3, 0], unit: "hour" } }
}
}
}
]);
In MongoDB, seasonal patterns can be observed by grouping data based on days, months, or years.
db.temperatureReadings.aggregate([
{
$group: {
_id: { month: { $month: "$timestamp" } },
avgTemperature: { $avg: "$temperature" }
}
},
{ $sort: { "_id.month": 1 } }
]);
MongoDB’s indexing options ensure efficient data retrieval for time series collections.
db.temperatureReadings.createIndex({ timestamp: 1, location: 1 });
Visualizing time series data helps identify trends and patterns. MongoDB integrates with visualization tools like MongoDB Charts, Grafana, and Tableau.
Time series data is invaluable across various fields. MongoDB’s specialized time series collections and powerful aggregation framework make it an ideal database for storing, managing, and analyzing large volumes of time-stamped data. By following best practices for schema design, indexing, and visualization, MongoDB users can gain critical insights from their time series data efficiently and at scale. Happy coding !❤️