{"success":true,"name":"use-mongodb-time-series-or-bucketing-for-metric-like-data","description":"Storing measurements, events, or readings over time in MongoDB","category":"ai-brimind","path":"ai-brimind/use-mongodb-time-series-or-bucketing-for-metric-like-data/SKILL.md","file":"SKILL.md","content":"---\nname: use-mongodb-time-series-or-bucketing-for-metric-like-data\ndescription: \"Storing measurements, events, or readings over time in MongoDB\"\n---\n\n# Use MongoDB Time Series or Bucketing for Metric-Like Data\n\n## When to use\nProject uses MongoDB, and the app records timestamped streams: sensor readings, metrics, activity events, price ticks.\n\n## Example request\nOne document per sensor reading in a normal collection → millions of tiny docs, huge indexes; time series collections exist for exactly this shape. Creating the collection first, learning about timeseries options later → the type is chosen at creation (timeField, metaField, granularity); it can't be flipped on afterward. On an older MongoDB without time series support → the bucket pattern (one doc per device per hour holding an array of readings) is the classic manual version. Querying 'average per day per device' → $dateTrunc + $group is the idiom either way. Each maps to 'time-stamped streams are a named data shape with a named storage answer'.\n\n## Instructions\nAct as a data architect who recognizes time-stamped measurement streams as a special shape before creating an ordinary collection for them. When the data is 'many small readings over time': (1) name the shape out loud — time series — because MongoDB has a dedicated collection type for it: created with timeseries options (timeField for the timestamp, metaField for the source like deviceId, granularity matching the ingest rate), giving compressed storage and faster time-range queries for free, (2) know the constraint that makes this a day-one decision: a collection's time series nature is set at CREATION — check the server version supports it and decide before the first insert, not after millions of documents, (3) if time series collections aren't available (old server version), apply the manual classic — the bucket pattern: group readings into one document per source per time window (e.g. per hour), appending into a bounded array — which is the same idea hand-built, (4) pair the storage with retention: metric data usually expires; TTL or the timeseries expireAfterSeconds option belongs in the same design conversation. If ingest volume and retention are unknown, ask — they size everything. Only create the collection once the shape is matched to its mechanism, and explain why storage and queries stay efficient at millions of readings.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1156\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}