Working with Time Series Collections
1. Creating Time Series Collection
| Field | Description |
|---|---|
| timeseries.timeField | Required Date field |
| timeseries.metaField | Optional grouping field |
| timeseries.granularity | "seconds" | "minutes" | "hours" |
| expireAfterSeconds | Auto-expire old data |
db.createCollection("metrics", {
timeseries: { timeField: "ts", metaField: "sensorId", granularity: "seconds" },
expireAfterSeconds: 2592000
});
2. Setting Time Field
| Rules | Detail |
|---|---|
| Required | Must exist in every doc |
| Type | BSON Date |
| Immutable | Cannot update after insert |
3. Setting Meta Field
| Aspect | Detail |
|---|---|
| Purpose | Groups related measurements (sensor, device id, symbol) |
| Type | Any BSON type (often subdocument) |
| Updates | Allowed (5.0.5+) on meta only |
| Indexes | Automatic compound (metaField, timeField) |
4. Configuring Granularity
| Value | Bucket Span |
|---|---|
| seconds (default) | 1 hour |
| minutes | 24 hours |
| hours | 30 days |
| bucketMaxSpanSeconds / bucketRoundingSeconds NEW | Custom (6.3+) |
5. Inserting Time Series Data
| Method | Detail |
|---|---|
| insertOne / insertMany | Standard write API |
| Bulk insert | Recommended for high-throughput |
| Backfill | Supported but reduces bucket efficiency |
6. Querying Time Series Data
| Pattern | Detail |
|---|---|
| find() | Returns individual measurements (unbucketed) |
| Range on time | Highly optimized via bucket index |
| Meta filter | Indexed by default |
7. Using Aggregation with Time Series
| Pattern | Operator |
|---|---|
| Downsampling | $dateTrunc + $group |
| Moving avg | $setWindowFields + $avg |
| Gap filling | $densify + $fill |
8. Setting Expiration
| Option | Effect |
|---|---|
| expireAfterSeconds | Per-bucket TTL based on timeField |
| Modify | collMod with expireAfterSeconds |
9. Understanding Bucketing Behavior
| Aspect | Detail |
|---|---|
| Internal storage | One BSON doc per (metaField, time-bucket) |
| Bucket cap | 1000 measurements OR 128KB |
| Compression | Columnar compressed (7.0+) |
10. Optimizing Time Series Performance
| Tip | Effect |
|---|---|
| Insert in time order | Maximizes bucket reuse |
| Stable metaField | Avoid high-cardinality random meta |
| Correct granularity | Match data arrival rate |
| Bulk writes | Amortize cost |
11. Creating Secondary Indexes on Time Series
| Allowed | Notes |
|---|---|
| On metaField + timeField | Auto-created |
| On measurement fields | Allowed; affects bucket index efficiency |
| Geo (2dsphere) | Supported (5.0.5+) |
| TTL | Use collection-level expireAfterSeconds |