$densify (Densify)
Last updated: 2026-10-09
$densify (Densify)
$densify generates documents at missing intermediate field values, typically for time-series interpolation — filling gaps at fixed hourly, daily, or other steps. Numeric fields are supported too. Specify the field, a range (step, unit, bounds), and optional partitionByFields.
| Category | Grouping & Window |
|---|---|
| MongoDB Version | 5.1+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $densify: { field: "<field>", range: { step: <step>, unit: <unit>, bounds: <bounds> }, partitionByFields: [ ... ] } }
⚙️ Parameters
| field | Field to densify; must be numeric or a date. |
|---|---|
| range | Object defining step, unit (for dates), and boundary range. |
| partitionByFields | Partition fields; each partition is densified independently. |
Returns:Document stream with missing values filled in
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $densify: fill temperature readings hourly
db.temperature.aggregate([
{ $densify: {
field: 'date',
range: { step: 1, unit: 'hour', bounds: 'full' }
} }
])
Output:
// Missing hours are filled; other fields are copied
📌 Notes
- Only numeric and date fields are supported
- partitionByFields densifies each partition independently
- bounds can be full, partition bounds, or an explicit array
🏷️ Related Tags
💡 Related Cases
📚 Related Tutorials
❓ FAQ
QWhat is $densify used for?
AIt fills missing time points at a fixed step so charts and interpolations work on complete series.
QWhich units are available?
ATime units include minute, hour, day, week, month, quarter, year, and more.
QHow many documents does $densify produce?
AIt depends on step and bounds: wide ranges with small steps yield many documents, so watch memory.