Stop Recomputing! Use MongoDB’s $out Stage to Save Aggregation Results Forever

Darshit AnjariaDarshit Anjaria
4 min read

Hey devs! 👋

Need to save the results of your aggregation into a new or existing collection? MongoDB’s $out stage has got your back! 💾

It lets you export the final pipeline output directly into a collection—perfect for generating reports, precomputing analytics, or archiving processed data. Think of it as a "write-to-collection" button at the end of your pipeline.

Let’s break it down! 🚀


When working with MongoDB Aggregation Pipelines, most stages are used to transform or filter data within the pipeline. But what if you wanted to store the final output into a new or existing collection for further querying, reporting, or backup?

That’s where the $out stage steps in — a powerful and lesser-used stage that can make your data pipeline smarter and more efficient.

Let’s break it down.


What is MongoDB’s $out Stage? 📌

In simple terms, $out takes the final result of your aggregation pipeline and writes it directly into a collection.

  • If the target collection doesn't exist, MongoDB creates it.

  • If the target collection exists, MongoDB replaces all documents in it with the new ones from the pipeline.

Syntax:

{
  $out: "targetCollectionName";
}

It must be the last stage in your aggregation pipeline.


Why Use $out? 🔍

Here are a few reasons developers use $out:

  • ✅Save aggregated results for reporting dashboards

  • ✅ Backup data based on a snapshot

  • ✅ Precompute heavy transformations for fast querying

  • ✅ Archive processed data into another collection

It's like saying: “Hey MongoDB, once you're done aggregating, go ahead and save that result for me.


Real-World Scenario: Daily Sales Report Generation 🌍

Let’s say you're building an e-commerce application. Each time a sale happens, it’s recorded in a sales collection with fields like:

{
  "_id": ObjectId("..."),
  "product": "Laptop",
  "amount": 1200,
  "date": ISODate("2025-04-11T10:00:00Z"),
  "region": "North America"
}

Now, your team needs a daily sales summary stored in a separate collection called daily_sales_summary for reporting tools to consume.


Using $out in an Aggregation Pipeline 🛠️

Here’s how you can build the pipeline:

db.sales.aggregate([
  {
    $match: {
      date: {
        $gte: ISODate("2025-04-10T00:00:00Z"),
        $lt: ISODate("2025-04-11T00:00:00Z"),
      },
    },
  },
  {
    $group: {
      _id: "$region",
      totalSales: { $sum: "$amount" },
      numberOfOrders: { $sum: 1 },
    },
  },
  {
    $project: {
      region: "$_id",
      totalSales: 1,
      numberOfOrders: 1,
      _id: 0,
    },
  },
  {
    $out: "daily_sales_summary",
  },
]);

What This Does 🧠:

  1. Filters sales from a specific day

  2. Groups data by region

  3. Projects a cleaner output format

  4. Stores the result in daily_sales_summary

Now, daily_sales_summary will have documents like:

{
  "region": "North America",
  "totalSales": 51200,
  "numberOfOrders": 48
}

This is now queryable independently and can be exposed to analytics dashboards without re-running heavy aggregations each time.


Things to Know Before Using $out 🚧

  • 🔄 It replaces the target collection entirely. Use with caution if the collection has valuable data.

  • 🔒 Requires write access to the target database/collection.

  • ❌ Cannot be used inside a transaction or when reading from a sharded collection (in older versions).

  • 🧪 Available only in aggregation pipelines, not in queries.


Where $out Shines 💡

Here are some places where $out is a great fit:

Use CaseWhy $out Helps
Precomputing analytics dataFaster front-end dashboards
ETL pipelinesStage-wise data persistence
Archiving filtered dataSnapshot for audits or reports
Complex joins or transformationsSave intermediate or final results

Final Thoughts 🧭

MongoDB’s $out stage is a hidden gem that can boost performance, enable better data workflows, and make your architecture cleaner. Whether you’re preparing reports, saving intermediate results, or automating snapshots, $out gives you full control over where your data lands after it’s processed.

So next time you find yourself asking, “How do I store this pipeline result?”, remember $out has your back!

👉 Have you used $out in your project? Let us know how it helped streamline your workflow!


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

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