Knowledge base · Studio 3T Desktop IDE

MongoDB $out Example | The MongoDB Aggregation Pipeline

By Kathryn Vargas · Updated

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:

In case you haven’t yet, download Studio 3T here.

Watch the video on Vimeo

$out syntax

When 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 Add stage. Choose $out in the Operator dropdown menu.

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

Replace the code, making sure to remove the curly braces, with:

"customers-affordable-housing"

This is the collection name where we’d like to store our aggregation query’s output documents.

Click on the Run button in the toolbar to run the full pipeline.

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

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

db.getCollection("customers").aggregate(
    [
        {
            "$lookup" : {
                "from" : "housing",
                "localField" : "address.zip_code",
                "foreignField" : "Zip Code",
                "as" : "address.zip_code.affordable_housing_options"
            }
        },
        {
            "$project" : {
                "first" : 1.0,
                "last" : 1.0,
                "address.city" : 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:

With your consent, we and third-party providers use cookies and similar technologies on our website to analyse your use of our site for market research or advertising purposes, including personalized advertising (“performance”), to provide you with additional functions (“functional”), and to show content from social media platforms, such as embedded videos (“social media”). This may result in the creation of pseudonymous usage profiles and the transfer of personal data to third countries, including the USA, which may have no adequate level of protection for the processing of personal data. Please choose for which purposes you wish to give us your consent and store your preferences by clicking on “Accept selected”. You can find further information in section 9 of our Privacy Policy.

Manage consent preferences