$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.

CategoryGrouping & Window
MongoDB Version5.1+
Example Languagemongosh (Shell)

📝 Syntax

{ $densify: { field: "<field>", range: { step: <step>, unit: <unit>, bounds: <bounds> }, partitionByFields: [ ... ] } }

⚙️ Parameters

fieldField to densify; must be numeric or a date.
rangeObject defining step, unit (for dates), and boundary range.
partitionByFieldsPartition 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

📚 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏