Try Studio 3T for Free
  1. Knowledge Base
  2. MongoDB Aggregation Framework
  3. MongoDB $out Example | The MongoDB Aggregation Pipeline

MongoDB $out Example | The MongoDB Aggregation Pipeline

This is Stage 3 of a three-part aggregation query example that uses the $lookup, $project$out stages and Studio 3T. Download it here.

The $out stage

The $out stage is used to export output documents of an aggregation query to a new collection. It must always be last stage of your aggregation pipeline.

To illustrate how a full aggregation query works with an $out stage, we recommend building other stages first:

$out syntax

Once you’re ready to save your aggregation pipeline results, you can write the $out command which has a straightforward syntax:

{ $out: "<output-collection>" }

Replace <output-collection> with your desired new collection name.

$out in Aggregation Editor

Follow the steps in our $project stage example. To finish off our three-stage example, we will add a third and final stage to our pipeline.

Click on the green plus icon. This will open a new tab called Stage 3.

Choose $out from the dropdown menu. 

MongoDB $out stage should always be the last stage in the pipeline


All we need to do is replace <output-collection> – make sure to remove the brackets but keep the quotes! – and type the collection name where we’d like to store our aggregation query’s output documents. 

Let’s type customers-affordable-housing and click on the play button in the toolbar to execute the full pipeline.

Execute the $mongodb out stage


Refresh your current database by right-clicking and choosing Refresh Selected Item or Refresh All.

You should then see your new collection on the list. 

New collection created by $out

$out in mongo shell

To see how $out looks like within the full MongoDB aggregation query, let’s go back to the Aggregation Editor and click on Query Code. 

The full mongo shell code with the $out stage

You should see the mongo shell code with the $out stage included, which you can easily paste into IntelliShell.

            "$lookup" : {
                "from" : "housing", 
                "localField" : "address.zip_code", 
                "foreignField" : "Zip Code", 
                "as" : "address.zip_code.affordable_housing_options"
            "$project" : {
                "first" : 1.0, 
                "last" : 1.0, 
                "" : 1.0, 
                "address.state" : 1.0, 
                "address.zip_code.affordable_housing_options.Property Type" : 1.0, 
                "address.zip_code.affordable_housing_options.Property Name" : 1.0, 
                "address.zip_code.affordable_housing_options.Units" : 1.0, 
                "address.zip_code.affordable_housing_options.Zip Code" : 1.0
            "$out" : "customers-affordable-housing"
        "allowDiskUse" : false

Now that you’ve reached the end of our three-part aggregation query example:

Updated on December 2, 2020

Was this article helpful?

Related Articles