Knowledge base · Studio 3T Desktop IDE

MongoDB $project Example | The MongoDB Aggregation Pipeline

By Kathryn Vargas · Updated

This is Stage 2 of a three-part aggregation query example that uses the $lookup, $project & $out stages. Download Studio 3T to follow along the example.

Watch the video on Vimeo

The $project stage

Sometimes we need to do a bit of data clean-up in our collection.

Picking up from Stage 1 – our $lookup example – we definitely don’t need all the fields about the customer or affordable housing options.

The JSON document from the first part of the aggregation query example

JSON document (feat. data we don’t need)

The $project stage is extremely useful for filtering a document to show only the fields we need:

  • first
  • last
  • address.city
  • address.state
  • address.zip_code.affordable_housing_options.Property Type
  • address.zip_code.affordable_housing_options.Property Name
  • address.zip_code.affordable_housing_options.Units
  • address.zip_code.affordable_housing_options.Zip Code

Highlighting the fields we'll filter using $project

$project syntax

The $project syntax is quite straightforward. We need to indicate 1 or true as the value to include the field in our projection.

<field> : <1 or true>

The nature of an aggregation pipeline is to have multiple stages, so you will likely use $project in tandem with other operators like $find, $match, or $lookup.

$project in Aggregation Editor

Download Studio 3T to access Aggregation Editor.

To illustrate how $project works, let’s build off of our $lookup stage example.

Let’s add a new stage by clicking on Add stage.

Choose $project in the Operator dropdown menu.

Click Add stage and then select the $project operator

Let’s paste the query:

{
    "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,
}

On the Stage input/output tab, click on the Run (play) button under Stage2: Output to run just this stage.

Or execute the entire pipeline by clicking on the Run button in the toolbar.

The resulting documents only show the fields we wanted to project

The pipeline and stage outputs should now only show the fields we want.

$project in mongo shell

To view the aggregation queries in mongo shell code, click on Query Code.

mongo shell code with lookup and project stages

You can then open this query directly in IntelliShell by clicking on the Open in IntelliShell button.

Alternatively, you can paste the following query in IntelliShell, which should get you the same results:

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
            }
        }
    ],
    {
        "allowDiskUse" : false
    }
);

Now, let’s add a third stage and save these output documents to a new collection using the $out stage.

Read next: Stage 3 – MongoDB $out stage

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