Skip to content
Studio 3T - The professional GUI, IDE and client for MongoDB
  • Tools
    • Aggregation Editor
    • IntelliShell
    • Visual Query Builder
    • Export Wizard
    • Import Wizard
    • Query Code
    • SQL Query
    • Connect
    • Schema Explorer
    • Compare
    • SQL ⇔ MongoDB Migration
    • Data Masking
    • Task Scheduler
    • Reschema
    • More Tools and Features
  • Solutions
  • Resources
    • Knowledge Base
    • MongoDB Tutorials & Courses
    • Tool/Feature Documentation
    • Reports
    • Case Studies
    • Whitepapers
    • Blog
    • Testimonials
    • Community
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Career
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • My License
  • Download
search

Studio 3T Knowledge Base

  • Documentation
  • Tutorials
  • Workshops
Take the fastest route to learning MongoDB. Cover the basics in two hours with MongoDB 101, no registration required.
Start the free course

MongoDB $project Example | The MongoDB Aggregation Pipeline

Posted on: 26/11/2019 (last updated: 09/03/2022) by Kathryn Vargas

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.

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.

Two embedded documents, two affordable housing options

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
Use $project to filter down to the fields you need
No video selected or not a valid video source.EMBED ERROR: Please specify a video source.

$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 to Aggregation Editor.

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

Do steps 1-4, then let’s add a new stage by clicking on the green plus icon. This will open a new stage tab called Stage 2.

Choose $project in the dropdown menu.

Click on the green plus icon to add a new stage

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

Click on the play button under Stage Output to execute just this stage.

Stage output of the project stage

Or execute the entire pipeline by clicking on the play 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 query’s in mongo shell code, click on Query Code and choose mongo shell from the dropdown.

mongo shell code with lookup and project stages

You can then run 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


How helpful was this article?
This article was hideous
This article was bad
This article was ok
This article was good
This article was great
Thank you for your feedback!

About The Author

Kathryn Vargas

When she's not writing about working with MongoDB, Kathryn spends her free time exploring Berlin's food scene, playing the drums, learning languages (current mission: German), and hiking.

Article navigation

Related articles

  • MongoDB $out Example | The MongoDB Aggregation Pipeline
  • MongoDB $lookup Example | The MongoDB Aggregation Pipeline
  • Test your skills: Working with the MongoDB Aggregation Pipeline
  • 3 Best MongoDB Aggregation Pipeline Builders
  • Working with the MongoDB Aggregation Pipeline

Studio 3T

MongoDB Enterprise Certified Technology PartnerSince 2014, 3T has been helping thousands of MongoDB developers and administrators with their everyday jobs by providing the finest MongoDB tools on the market. We guarantee the best compatibility with current and legacy releases of MongoDB, continue to deliver new features with every new software release, and provide high quality support.

Find us on FacebookFind us on TwitterFind us on YouTubeFind us on LinkedIn

Education

  • Free MongoDB Tutorials
  • Connect to MongoDB
  • Connect to MongoDB Atlas
  • Import Data to MongoDB
  • Export MongoDB Data
  • Build Aggregation Queries
  • Query MongoDB with SQL
  • Migrate from SQL to MongoDB

Resources

  • Feedback and Support
  • Sales Support
  • Knowledge Base
  • FAQ
  • Reports
  • Case Studies
  • White Papers
  • Testimonials
  • Discounts

Company

  • About Us
  • Blog
  • Careers
  • Legal
  • Press
  • Privacy Policy
  • EULA

© 2022 3T Software Labs GmbH. All rights reserved.

  • Privacy Policy
  • Cookie settings
  • Impressum
When you click "Accept", you are agreeing to cookies being on your device. They may improve site navigation, site usage analysis, or the relevance of messages. It is up to you which cookies are enabled. Read our Privacy Policy.
Manage cookies
Accept
✕

Privacy Preference Center

A cookie is a small file of letters and numbers that is downloaded on to your computer when you visit a website. Cookies are used by many websites and can do a number of things, eg remembering your preferences, recording what you have put in your shopping basket, and counting the number of people looking at a website. In the form below you can choose which cookies, except for essential cookies, to allow or disable.

Accept all cookies

Manage consent preferences

Essential cookies are strictly necessary to provide an online service such as our website or a service on our website which you have requested. The website or service will not work without them.

Performance cookies allow us to collect information such as number of visits and sources of traffic. This information is used in aggregate form to help us understand how our websites are being used, allowing us to improve both our website’s performance and your experience.

Functional cookies collect information about your preferences and choices and make using the website a lot easier and more relevant. Without these cookies, some of the site functionality may not work as intended.

Social media cookies are cookies used to share user behaviour information with a third-party social media platform. They may consequently effect how social media sites present you with information in the future.

Accept selected