A monitoring platform can generate millions of measurements every day. Each application instance may record CPU usage, memory consumption, API response times, request counts, and error rates at regular intervals. As the number of monitored services increases, the database must store an increasingly large volume of time-based information.
The challenge is not simply the number of documents. Every document also contains field names, timestamps, metadata, BSON structure, and index information. When similar measurements are stored independently, the repeated information can consume a considerable amount of storage.
MongoDB Time Series Collections address this problem through automatic bucketing and compression. Instead of treating every measurement as an entirely separate storage unit, MongoDB groups measurements with matching metadata and nearby timestamps into internal buckets. It then applies column-oriented compression techniques to reduce the amount of information stored.
For developers building monitoring systems, IoT platforms, financial data services, or analytics applications, understanding this process can help reduce storage requirements without sacrificing the ability to query individual measurements.
This article explains how MongoDB bucket compression works, how to design documents for better compression, how bucket configuration affects storage efficiency, and how to measure the results.
Why Time Series Data Can Consume So Much Storage
Consider an application that records API performance every minute. Each measurement contains a timestamp, service name, server identifier, response time, CPU usage, memory consumption, and request count.
A typical document might look like this:
{
timestamp: ISODate("2026-11-24T10:00:00Z"),
metadata: {
service: "OrderAPI",
instance: "server-01",
environment: "Production"
},
responseTimeMs: 125,
cpuUsage: 62.5,
memoryUsageMB: 1024,
requestCount: 340
}The next measurement from the same server may contain almost the same structure:
{
timestamp: ISODate("2026-11-24T10:01:00Z"),
metadata: {
service: "OrderAPI",
instance: "server-01",
environment: "Production"
},
responseTimeMs: 132,
cpuUsage: 63.1,
memoryUsageMB: 1030,
requestCount: 355
}Only the timestamp and measurement values have changed. The metadata and document structure remain identical.
If the application stores every measurement in a regular collection, MongoDB must manage these as separate documents. The repeated metadata and field structure contribute to storage overhead, and indexes must also be maintained for the individual records.
This becomes particularly expensive when the application monitors thousands of servers and collects measurements continuously.
Time series collections reduce this overhead by grouping related measurements into buckets and storing their values in a format designed for time-based data.
Understanding MongoDB Bucket Storage
When you create a time series collection, MongoDB automatically manages internal buckets.
A bucket groups measurements that have:
- Identical metadata values.
- Timestamp values within the bucket's permitted time range.
- Available capacity within the bucket.
For example, measurements from OrderAPI running on server-01 may be grouped together, while measurements from PaymentAPI running on server-02 are placed in a different group.
MongoDB stores bucket information internally, including control information about the time range and the measurements represented by the bucket.
Conceptually, instead of storing three independent documents like this:
{
timestamp: ISODate("2026-11-24T10:00:00Z"),
metadata: {
service: "OrderAPI",
instance: "server-01"
},
responseTimeMs: 125,
cpuUsage: 62.5
}{
timestamp: ISODate("2026-11-24T10:01:00Z"),
metadata: {
service: "OrderAPI",
instance: "server-01"
},
responseTimeMs: 132,
cpuUsage: 63.1
}{
timestamp: ISODate("2026-11-24T10:02:00Z"),
metadata: {
service: "OrderAPI",
instance: "server-01"
},
responseTimeMs: 118,
cpuUsage: 61.8
}MongoDB can organize the measurements internally into one bucket containing shared metadata and separate sequences of values.
This organization reduces repeated information and gives MongoDB an opportunity to compress related values together.
A bucket is an internal storage structure, not a separate collection that application developers need to manage manually. Applications continue inserting and querying measurements through the logical time series collection.
How MongoDB Compresses Time Series Buckets
MongoDB uses specialized compression for time series buckets. Starting with MongoDB 5.2, time series collections use column compression, which improves compression by processing values from the same fields together.
Time series collections use Zstandard (zstd) compression by default rather than the general WiredTiger default of Snappy.
Several techniques work together to reduce the amount of stored information.
Delta Encoding
Delta encoding stores the difference between consecutive values instead of repeatedly storing the full values.
Consider the following response times:
125
128
130
127
132The values change only slightly between measurements.
Instead of representing every value independently, compression can represent the first value and the differences that follow:
125
+3
+2
-3
+5This can require less information than storing every original value, particularly when measurements change gradually.
Delta encoding is especially useful for timestamps and numeric measurements that have relatively small changes between consecutive records.
Delta-of-Delta Encoding
Some time series values increase in a predictable sequence.
For example, measurements collected every minute may have timestamps like these:
10:00:00
10:01:00
10:02:00
10:03:00
10:04:00The difference between consecutive timestamps is consistently 60 seconds.
Delta-of-delta encoding takes advantage of this pattern by storing changes in the differences themselves.
If the difference remains constant, the change between differences is zero. This can make timestamp sequences particularly efficient to represent.
This technique is useful for time-based data because many monitoring systems collect measurements at regular intervals.
Run-Length Encoding
Run-length encoding compresses repeated values by representing a sequence of identical values more compactly.
For example, a CPU measurement might contain the following values:
45
45
45
45
45
48
48
48Instead of storing every repeated value independently, compression can represent the repeated sequences more efficiently.
This is particularly useful for measurements that remain stable for several collection intervals.
Object and Array Compression
MongoDB also supports compression of nested objects and arrays within time series documents.
For example, an application may record multiple resource measurements:
{
timestamp: ISODate("2026-11-24T10:00:00Z"),
metadata: {
service: "OrderAPI"
},
resources: {
cpu: 62.5,
memory: 1024,
disk: 78
}
}Column compression processes nested fields individually, allowing the measurements to benefit from compression even when values are stored inside embedded objects.
Array compression is also supported starting with MongoDB 6.0.
These techniques mean developers do not necessarily need to flatten every document simply to obtain compression benefits. However, schema consistency and metadata design still matter.
Creating a Time Series Collection With Appropriate Bucket Settings
Let's create a collection for application performance measurements.
Assume the monitoring service records metrics from each application instance approximately once every five minutes.
A suitable starting point is the minutes granularity.
db.createCollection(
"applicationMetrics",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "minutes"
}
}
);The timeField identifies when each measurement was collected. The metaField identifies the source of the measurement, and granularity helps MongoDB determine an appropriate bucket time range.
MongoDB provides three predefined granularity settings:
| Granularity | Maximum bucket time span |
|---|---|
| seconds | 1 hour |
| minutes | 24 hours |
| hours | 30 days |
These values describe the maximum time span represented by a bucket. They do not guarantee that every bucket will remain open for that entire period or reach its maximum capacity.
For measurements collected every five minutes, minutes is generally a more appropriate starting point than seconds or hours.
Using seconds can create many smaller buckets when measurements arrive less frequently. Using hours can create buckets covering much larger time ranges than typical queries need.
The correct setting depends on how frequently measurements arrive from the same source and how the application queries historical data.
Using Custom Bucket Intervals
Starting with MongoDB 6.3, developers can define custom bucket intervals using bucketMaxSpanSeconds and bucketRoundingSeconds instead of the predefined granularity setting.
For example, if the application records measurements every five minutes, you can configure a five-minute bucket interval:
db.createCollection(
"applicationMetrics",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
bucketMaxSpanSeconds: 300,
bucketRoundingSeconds: 300
}
}
);Both values must be specified together and must be equal.
The bucketMaxSpanSeconds option controls the maximum time span between measurements in a bucket. The bucketRoundingSeconds option controls how MongoDB rounds the timestamp when determining the start of a new bucket.
This gives developers more precise control over bucket time boundaries.
However, a five-minute interval is not automatically better simply because measurements arrive every five minutes. If measurements are frequently delayed or arrive out of order, the actual bucket distribution may differ from expectations.
Custom intervals should be selected after examining ingestion patterns and query requirements.
Designing Metadata for Better Compression
Metadata has a major influence on how MongoDB groups measurements into buckets.
Consider these documents:
{
timestamp: ISODate("2026-11-24T10:00:00Z"),
metadata: {
service: "OrderAPI",
instance: "server-01",
environment: "Production"
},
responseTimeMs: 125
}{
timestamp: ISODate("2026-11-24T10:01:00Z"),
metadata: {
service: "OrderAPI",
instance: "server-01",
environment: "Production"
},
responseTimeMs: 132
}Both documents have identical metadata, so MongoDB can group them together when their timestamps and bucket capacity allow it.
Now consider changing the metadata for every measurement:
{
timestamp: ISODate("2026-11-24T10:00:00Z"),
metadata: {
service: "OrderAPI",
instance: "server-01",
requestId: "REQ-10001"
},
responseTimeMs: 125
}{
timestamp: ISODate("2026-11-24T10:01:00Z"),
metadata: {
service: "OrderAPI",
instance: "server-01",
requestId: "REQ-10002"
},
responseTimeMs: 132
}The requestId changes for every measurement. This creates different metadata values and can prevent the records from being grouped into the same buckets.
The result may be many small buckets containing very few measurements.
For better compression, keep metadata focused on stable identifiers such as service name, environment, and server instance. Put request IDs, response times, status codes, and other changing values in ordinary measurement fields.
The goal is not to remove useful information. It is to separate information that identifies a time series from information that changes with every measurement.
Maintaining a Consistent Document Structure
Another important compression consideration is schema consistency.
Consider a collection where most documents contain the same fields:
{
timestamp: ISODate("2026-11-24T10:00:00Z"),
metadata: {
service: "OrderAPI"
},
cpuUsage: 62.5,
memoryUsageMB: 1024,
responseTimeMs: 125
}However, some documents contain empty objects or arrays:
{
timestamp: ISODate("2026-11-24T10:01:00Z"),
metadata: {
service: "OrderAPI"
},
cpuUsage: 63.1,
memoryUsageMB: 1030,
responseTimeMs: 132,
diagnosticData: {}
}An empty object or array can introduce schema changes in the compressed representation. MongoDB's time series compression guidance recommends omitting fields containing empty objects, empty arrays, or empty strings when they do not carry meaningful information.
Instead of inserting an empty diagnosticData object, omit the field:
{
timestamp: ISODate("2026-11-24T10:01:00Z"),
metadata: {
service: "OrderAPI"
},
cpuUsage: 63.1,
memoryUsageMB: 1030,
responseTimeMs: 132
}This keeps the document structure more consistent and gives compression a better opportunity to work efficiently.
Also maintain a consistent field order when using nested objects. Column compression depends on consistent nested field ordering. Applications using different language drivers should ensure that their document serialization does not introduce unnecessary variations in field order.
Measuring Storage Usage Before and After Optimization
Compression improvements should be measured rather than assumed.
MongoDB provides collection statistics through the stats command.
For example:
db.runCommand({
collStats: "applicationMetrics"
});The output contains information about the collection's storage, indexes, and other statistics.
For a more focused view:
db.applicationMetrics.stats();Important fields to examine include:
- count: The number of logical measurements.
- size: The logical size of the uncompressed documents.
- storageSize: The allocated storage size for the collection.
- totalIndexSize: The storage used by indexes.
- totalSize: The combined storage size of the collection and indexes, where reported.
For a time series collection, storage statistics should be interpreted with the internal bucket representation in mind. The collection's logical document count and the number of internal bucket records are not the same.
The following query can help inspect the bucket collection associated with a time series collection:
db.getCollection("system.buckets.applicationMetrics").stats();The internal bucket namespace is useful for diagnostic purposes, but application code should normally use the logical time series collection rather than directly modifying internal bucket documents.
When comparing compression results, use the same dataset, MongoDB configuration, index configuration, and measurement period. Otherwise, the comparison may not reflect the actual impact of a schema or bucket change.
Understanding the Impact of Indexes on Storage
Compression is not the only factor affecting total database storage.
Indexes also consume space, and their size can become significant when a collection contains millions of measurements.
MongoDB 6.3 and later automatically creates a compound index on the metaField and timeField for newly created time series collections.
You can inspect the collection's indexes using:
db.applicationMetrics.getIndexes();Additional indexes should be created only when they support important query patterns.
For example, if the monitoring dashboard frequently filters by service and timestamp, the automatically created compound index may already support that access pattern.
Creating additional indexes on every measurement field can increase storage consumption and add write overhead.
Before creating an index, examine the actual queries executed by the application and use explain to understand how MongoDB accesses the data.
The goal is to balance query performance with the storage cost of maintaining indexes.
Managing Data Retention Alongside Compression
Compression reduces the storage required for historical measurements, but it does not eliminate the need for a retention policy.
A monitoring platform may need detailed metrics for 30 days, aggregated hourly metrics for several months, and summary reports for longer periods.
MongoDB supports automatic expiration for time series collections through expireAfterSeconds.
For example, to retain measurements for 30 days:
db.createCollection(
"applicationMetrics",
{
timeseries: {
timeField: "timestamp",
metaField: "metadata",
granularity: "minutes"
},
expireAfterSeconds: 2592000
}
);The value 2592000 represents 30 days in seconds.
MongoDB removes expired measurements asynchronously through its background expiration process. This means that records may remain available for some time after their expiration threshold.
Retention should be based on business requirements, compliance obligations, and the cost of storing historical data.
For example, an operations team may need detailed one-minute measurements for recent troubleshooting but only hourly summaries for older periods. In that case, an aggregation process can create summary records in a separate collection before detailed measurements expire.
This approach combines compression with data lifecycle management instead of relying on compression alone.
Practical Checklist for Reducing Bucket Storage
When optimizing an existing time series collection, review the following areas:
- Use stable metadata values so measurements from the same source can share buckets.
- Select granularity based on the actual interval between incoming measurements.
- Consider custom bucket intervals when predefined settings do not match the ingestion pattern.
- Omit empty objects, arrays, and strings when they do not provide useful information.
- Keep document and nested-field structures consistent.
- Avoid placing unique request IDs or frequently changing values in metadata.
- Review secondary indexes and remove indexes that do not support meaningful query patterns.
- Configure retention based on application and business requirements.
- Measure logical data size, bucket storage, and index size before and after changes.
- Test compression and query performance using realistic ingestion patterns rather than a small sample dataset.
These practices work together. A good bucket interval cannot compensate for metadata that changes on every measurement, and a consistent schema cannot eliminate the storage cost of unnecessary indexes.
Conclusion
MongoDB bucket compression helps reduce the storage overhead of applications that continuously generate timestamped measurements. By grouping records with matching metadata and nearby timestamps, MongoDB can store related values in a column-oriented format and apply techniques such as delta encoding, delta-of-delta encoding, and run-length encoding.
The greatest improvements come from designing the data model around the characteristics of the workload. Stable metadata, consistent document structures, appropriate bucket intervals, and carefully selected indexes all contribute to efficient storage.
For production monitoring systems, developers should measure bucket storage and index growth, review ingestion patterns, and define a clear retention strategy. Combining these practices allows MongoDB time series collections to handle large historical datasets more efficiently while keeping measurements accessible for operational analysis.

Join the conversation! Your thoughts help the community grow.