Time-series collections (5.0+) are MongoDB’s purpose-built storage for high-volume, append-only data with a timestamp and a few identifying tags — sensor readings, application metrics, financial ticks. Under the hood they implement the bucket pattern automatically: documents are grouped into time-windowed buckets, stored in columnar form, and exposed as if they were regular documents. This article covers when to use them, the constraints they impose, and the operations that behave differently.
How to verify
Create a time-series collection and inspect what the server does behind the scenes:
mongosh --host primary.example.internal:27017
use telemetry
db.createCollection("readings", {
timeseries: { timeField: "ts", metaField: "sensor", granularity: "seconds" },
expireAfterSeconds: 2592000 // 30 days
})
db.readings.insertMany([
{ ts: new Date(), sensor: "s1", temp: 20.1 },
{ ts: new Date(), sensor: "s1", temp: 20.2 }
])
db.system.buckets.readings.find().limit(1).pretty()
The system.buckets.readings collection contains the raw bucketed form — each document is a bucket holding many measurements packed into arrays. Queries against db.readings see the unpacked, per-measurement view.
What’s happening
A time-series collection is two collections in one. The user-facing collection (readings) accepts and returns individual documents — one per measurement. Behind it, MongoDB maintains system.buckets.readings, where measurements with the same metaField value and timestamps within the same bucket window are packed into a single document. A bucket document holds arrays of values (one per measured field) instead of a list of separate documents.
This shape gives compression. Time-series workloads typically have many sequential measurements with small deltas between them; arrays of similar values compress 5-10× better than per-document storage. The columnar packing also speeds up aggregations — $group by time window can scan one bucket document instead of thousands of raw documents.
The trade-off: time-series collections are not regular collections. You cannot update an individual measurement (only delete with deleteMany). You cannot use the standard transaction model across multiple measurements. The metaField cannot change after a document is inserted. Indexes work but are bucket-aware — indexed lookups identify candidate buckets, then unpacking happens at read time.
The procedure
-
Pick the right
timeField,metaField, andgranularity.granularitycontrols the bucket window —secondsfor sub-minute data,minutesfor sub-hour,hoursfor sub-day. Get this wrong and buckets are either too small (lots of small buckets, low compression) or too large (large buckets, slow reads).db.createCollection("metrics", { timeseries: { timeField: "timestamp", metaField: "tags", granularity: "minutes" } }) -
Use
tagsas the metaField. Group your identifying labels — sensor ID, host, region — into a single document undermetaField. The bucketing logic uses the exact metaField value as the bucket key.db.metrics.insertOne({ timestamp: ISODate("2026-06-02T12:34:56Z"), tags: { host: "web-1", region: "us-east-1", metric: "cpu_pct" }, value: 42.3 }) -
Set TTL with
expireAfterSeconds. Time-series collections support automatic expiry — old buckets are deleted when their bucket window’s upper bound exceeds the TTL. -
Index on
metaFieldsubfields andtimeFieldtogether. Most queries filter by tag value and time window; the compound index supports both.db.metrics.createIndex({ "tags.host": 1, timestamp: -1 }) -
Aggregate with time-window stages.
$groupby computed time-bucket gives the canonical “5-minute averages” query.db.metrics.aggregate([ { $match: { "tags.host": "web-1", timestamp: { $gte: ISODate("2026-06-01") } } }, { $group: { _id: { $dateTrunc: { date: "$timestamp", unit: "minute", binSize: 5 } }, avg: { $avg: "$value" } } }, { $sort: { _id: 1 } } ]) -
For higher write throughput, set
bucketMaxSpanSecondsandbucketRoundingSecondsexplicitly. Custom values override the granularity-inferred bucket bounds.
Operational notes
- Time-series collections cannot be sharded by arbitrary key. They can be sharded by
metaFieldvalue or bytimeField; the choice constrains how data distributes. Picking the wrong shard key forces an entire-collection re-creation later. - Updates and deletes are restricted. You can delete by query but cannot update individual measurements. Inserts are the dominant operation type.
- The bucket size matters for read performance. A bucket spanning 24 hours of 1-second readings holds 86 400 values; reading one minute of data still has to fetch and unpack the whole bucket. Pick
granularityso the typical query reads few buckets. metaFieldis the bucketing key. Two measurements with differentmetaFieldvalues go to different buckets. High-cardinalitymetaFieldvalues (millions of unique combinations) defeat compression because each bucket holds few values.- Backups treat them as regular collections.
mongodumpand snapshots include the bucketed form; restoration recreates the user-facing view automatically. - Schema validation is restricted on time-series collections. Most
$jsonSchemavalidators are not supported; enforce constraints in the application.
Stack Harbor designs time-series schemas for clients ingesting metrics, IoT sensor data, and financial market data into MongoDB — the metaField cardinality decision, the granularity vs query-pattern trade-off, the index set, and the TTL/retention strategy. See /en/services/managed-operations/ for the engagement model.