Data Looks Messy? Clean It Up Using $densify

Darshit AnjariaDarshit Anjaria
3 min read

Hey devs! 👋

Missing values in your time-series or numeric data? MongoDB’s $densify stage has your back! Let’s explore how it helps fill those gaps—automatically. 💡


Did you know MongoDB can fill in missing data points in your dataset automatically—without manual effort or writing complex loops? That’s exactly what the $densify stage does in the aggregation pipeline!

If you’ve ever worked with time-series data or any dataset with missing values, this stage can be a total game-changer.

Let’s break it down in simple terms.


What Is the $densify Stage in MongoDB? 🔍

The $densify stage is part of MongoDB's aggregation framework (introduced in version 5.1). It generates missing values in a sequence by inserting documents with interpolated or constant values for a specified field—commonly used for dates or numbers.

This is especially helpful when you're dealing with gaps in data, like missing days in time series logs, stock prices, sensor readings, or analytics data.


Why Use $densify? 💡

In real-world applications, data is rarely perfect. You might find yourself analyzing:

  • Sensor data where some timestamps are missing.

  • Daily sales logs where some days have no transactions.

  • Website traffic reports with dropped entries due to outages.

With $densify, you can automatically fill these gaps, making your datasets clean and analysis-ready.


Real-World Scenario: IoT Sensor Readings 🌐

Let’s say you’re building a dashboard for an IoT temperature sensor that logs data every hour. But due to network issues, some readings are missing. Here's how your data might look:

[
  { "timestamp": ISODate("2025-03-20T00:00:00Z"), "temperature": 22 },
  { "timestamp": ISODate("2025-03-20T03:00:00Z"), "temperature": 21 }
]

You want a full hourly log to power your graphs—even if some entries have no data. That’s where $densify comes in.


Example: Basic $densify Pipeline 🛠️

Here’s how you’d use $densify to fill in the hourly timestamps between missing readings:

db.sensor_data.aggregate([
  {
    $densify: {
      field: "timestamp",
      range: {
        step: 1,
        unit: "hour",
        bounds: "full",
      },
    },
  },
]);

What this does: 🔍

  • field: The field to densify—here it’s timestamp.

  • step: The step size between values (1 hour).

  • unit: The unit of time.

  • bounds: "full": Extends the densify range from the earliest to the latest timestamp in the collection.

The result will be:

[
  { "timestamp": ISODate("2025-03-20T00:00:00Z"), "temperature": 22 },
  { "timestamp": ISODate("2025-03-20T01:00:00Z") },
  { "timestamp": ISODate("2025-03-20T02:00:00Z") },
  { "timestamp": ISODate("2025-03-20T03:00:00Z"), "temperature": 21 }
]

Notice the auto-filled timestamps with missing temperature values? Now you can safely forward-fill, interpolate, or leave them as is depending on your use case.


Where and When to Use $densify 🧠

Use it when:

  • You’re working with time-series data and need continuity.

  • You need consistent intervals in charts or reports.

  • You're preparing data for machine learning, and need to handle missing entries.

Perfect for:

  • Analytics dashboards

  • Monitoring systems

  • Financial or IoT applications

  • Heatmaps or time-based visualizations


More Info Here 📚

For full documentation and advanced usage, check the official MongoDB docs:
👉 https://www.mongodb.com/docs/manual/reference/operator/aggregation/densify/


Final Thoughts ✅

The $densify stage might not be as widely known as $match or $group, but it solves very real problems in modern data pipelines. Whether you're working with analytics, IoT, or just want cleaner datasets—this feature can save hours of data wrangling.

Try it out, and see your missing data filled like magic!


Thank You!

Thank you for reading!
I hope you enjoyed this post. If you did, please share it with your network and stay tuned for more insights on software development. I'd love to connect with you on LinkedIn or have you follow my journey on HashNode for regular updates.

Happy Coding!
Darshit Anjaria

1
Subscribe to my newsletter

Read articles from Darshit Anjaria directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Darshit Anjaria
Darshit Anjaria

I’m a problem solver at heart, driven by the idea of building solutions that genuinely make a difference in people’s everyday lives. I’m always curious, always learning, and always looking for ways to improve the world around me through thoughtful, impactful work. Beyond building, I love giving back to the community — whether it’s by sharing what I’ve learned through blogs, tutorials, or helpful insights. My goal is simple: to make technology a little more accessible and useful for everyone. Let’s learn, build, and grow together.