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
    • Blog
    • Community
    • Testimonials
    • Whitepapers
    • Reports
  • Contact us
    • Contact
    • Sales Support
    • Feedback and Support
    • Careers
    • About Us
  • Store
    • Buy Now
    • Preferred Resellers
    • Team Pricing
  • Download
  • My 3T
search

Academy 3T

  • Explore our courses
    • MongoDB 101: Getting Started
    • MongoDB 201: Querying MongoDB Data
    • MongoDB 301: Aggregation
  • Get certified

Exercise 1: Adding lookup data to the aggregation pipeline

MongoDB 301: Aggregation Adding Lookup Data to the Aggregation Pipeline Exercise 1: Adding lookup data to the aggregation pipeline

In this exercise, you’ll import the states_transactions.js file into the Aggregation Editor. The file contains the aggregate statement that you updated in the second section of this course. You’ll use this statement as the foundation for all four exercises in this section, modifying the statement as you work through them. In this exercise, you’ll add lookup data to the aggregation pipeline, disable a stage, and modify two existing stages.

The exercises in this section have several requirements:

  • You should have already set up the sales database and customers collection. The first section in this course demonstrated how to create the database and import the collection. Refer to that section for details on how to set them up if you haven’t done so already.
  • You’ll need to download the population.json file and save it to a local drive. You can download the file population.json from that link. After you download the file, you should use it to create the population collection in the sales database. Refer to the first section in this course for details about importing a collection.
  • In the second section, you should have updated the aggregate statement you created in the first section and saved your changes to the states_transactions.js file. If you did not, you can still complete these exercises. Step 4 in this exercise provides a work-around for importing the statement. However, you must still set up the customers collection and population collection to complete these exercises.

To add lookup data to the pipeline

  1. Launch Studio 3T and connect to MongoDB Atlas.
  2. In the Connection Tree, expand the sales database node and, if necessary, expand the Collections node. 
  3. Right-click the customers collection node, and then click Open Aggregation Editor. Studio 3T adds the Aggregation tab to the main window. The tab displays the Aggregation Editor, with the editor’s Pipeline tab active. At this point, the aggregation pipeline is empty.
  4. On the Aggregation Editor toolbar, click the Open button (folder icon) on the left side, navigate to the folder that contains the states_transactions.js file, and double-click the file. Studio 3T uses the aggregate statement in that file to automatically populate the pipeline.
db.customers.aggregate(
  [
    { 
      "$match" : { 
        "dob" : { 
          "$lt" : ISODate("1970-01-01T00:00:00.000+0000")
        }
      }
    }, 
    { 
      "$group" : { 
        "_id" : "$address.state", 
        "total" : { 
          "$sum" : "$transactions"
        }
      }
    }, 
    { 
      "$project" : { 
        "_id" : 0.0, 
        "state" : "$_id", 
        "total" : 1.0
      
      }
    }, 
    { 
      "$replaceRoot" : { 
        "newRoot" : { 
          "state" : "$state", 
          "total" : "$total"
        }
      }
    }, 
    { 
      "$sort" : { 
        "state" : 1.0
      }
    }
  ], 
  { 
    "allowDiskUse" : false, 
    "collation" : { 
      "locale" : "en_US"
    }
  }
);

To add this statement to the Aggregation Editor, copy the statement to your clipboard and then paste it into the Aggregation Editor by clicking the Clipboard button at the right end of the Aggregation Editor toolbar.

Regardless of how you add the aggregate statement to the Aggregation Editor, the Pipeline tab should now list the aggregate statement’s five stages, as shown in the following figure. The listing for each stage includes the stage’s operator and its expression.

  1. On the Pipeline tab of the Aggregation Editor, right-click the $group pipeline stage, and then click Add New Stage After Selected Stage. 
    The Aggregation Editor adds a new tab to the right of the 2: $group tab and makes the new tab active. Initially, the new tab is named 3: $match because Studio 3T uses $match as its default operator when creating a new stage. 
  2. On the new tab, select $lookup from the Operator drop-down list near the tab’s upper left corner. The $lookup operator lets you incorporate data from another collection into the current pipeline. 
    When you select the $lookup operator, the Aggregation Editor changes the name of the tab to 3: $lookup and adds placeholder text.
  3. In the editor window, replace the placeholder text and curly braces with the following code:
{
  from: "population",
  localField: "_id",
  foreignField: "state",
  as: "state_info"
}

The expression includes four arguments, separated by commas. Each argument includes a field name and the value assigned to that field:

  • The from argument specifies that the lookup data should be retrieved from the population collection.
  • The localField argument specifies that the _id field in the customers collection should be used for matching values in the population collection.
  • The foreignField argument specifies that the state field in the population collection should be used for matching values in the customers collection.
  • The as argument specifies that the name state_info should be used for the array that will contain the data retrieved from the population collection.

In this case, the $lookup stage is performing an equity join. The join is based on the _id field in the customers collection and the state field in the population collection, making it possible to merge documents in the two collections and include the results in the aggregation pipeline.

  1. In the Stage Input pane, click the Execute button. Studio 3T runs the pipeline for the first two stages and returns the data to that pane. This is the data that is used as input for the $lookup stage.
  2. In the Stage Output pane, click the Execute button. This will execute the pipeline up to and including the $lookup stage. The following figure shows part of the results, as they appear in Tree View. (The documents might be displayed in a different order on your system.)

The output documents now include the state_info array. For each primary document, the array includes one element, which is an embedded document. The embedded document includes the fields from the matching document in the population collection. Notice that the _id value in the primary document matches the state value in the array.

  1. Go to the 2: $group tab and replace the total field name with transactions. This will make it easier to understand the data as the results become more complex. The operator expression should now look like the following code:
{ "_id": "$address.state", "transactions": { "$sum": "$transactions" } }
  1. Go to the 4: $project tab and clear the Include in the pipeline check box. This stage is not necessary with the $replaceRoot stage. Eventually, you will delete the $project stage, but for now keep it disabled. This is always a good practice when updating the pipeline, until you’re satisfied that you can safely delete the stage.
  2. Go to the 5: $replaceRoot tab and replace the existing operator expression with the following code:
{
  newRoot: {state: "$_id", transactions: "$transactions", population: "$state_info.population"}
}

The code changes the state value to $_id and changes the name of the total field to transactions. These changes are necessary because you updated the $group stage and disabled the $project stage. The newRoot operator also includes a third argument, which adds the $state_info.population field to the output. 

  1. In the Stage Input pane, click the Execute button. Studio 3T runs the pipeline for all stages up to but not including the $replaceRoot stage.
  2. In the Stage Output pane, click the Execute button. This will execute the pipeline up to and including the $replaceRoot stage. The following figure shows part of the results, as they appear in Tree View.

Notice that the state_info array and its embedded document have been replaced by the population array, which contains only one element, the population value.

  1. Click Save on the Aggregation Editor toolbar to save your changes.
  2. Leave the Aggregation tab open and the existing statement in place for the next exercise. You’ll be building on this statement by adding two more pipeline stages.

If you’re not ready to move on to the next stage, you can save the statement for now and close Studio 3T. You can then reopen the states_transactions.js file when you’re ready to move onto the next exercise.

Previous Lesson
Back to Lesson
Next Topic
  • Course Home Expand All
    Building a Basic Aggregation
    4 Topics | 1 Quiz
    Exercise 1: Filtering the documents in the aggregation pipeline
    Exercise 2: Grouping the documents in the aggregation pipeline
    Exercise 3: Sorting the documents in the aggregation pipeline
    Exercise 4: Adding processing options to the aggregation
    Building a Basic Aggregation: Test your skills
    Introducing the Aggregation Editor
    4 Topics | 1 Quiz
    Exercise 1: Importing an aggregate statement into the Aggregation Editor
    Exercise 2: Replace a field in the aggregation pipeline
    Exercise 3: Reorder the fields in the aggregation pipeline
    Exercise 4: Changing the sort order in the aggregation pipeline
    Introducing the Aggregation Editor: Test your skills
    Working with Arrays in the Aggregation Pipeline
    5 Topics | 1 Quiz
    Exercise 1: Using expression operators to filter input documents
    Exercise 2: Unwinding an array to create individual documents
    Exercise 3: Grouping array values and generating a document count for each group
    Exercise 4: Writing pipeline results to a new collection
    Working with Arrays in the Aggregation Pipeline: Test your skills
    MongoDB 301 Mid-Course Feedback
    Adding Lookup Data to the Aggregation Pipeline
    4 Topics | 1 Quiz
    Exercise 1: Adding lookup data to the aggregation pipeline
    Exercise 2: Converting string values in one of the lookup fields to integers
    Exercise 3: Adding a computed ratio field based on the converted lookup field
    Exercise 4: Limiting the number of returned documents
    Adding Lookup Data to the Aggregation Pipeline: Test your skills
    Working with Reschema for MongoDB
    4 Topics | 1 Quiz
    Exercise 1: Setting up a reschema unit that includes lookup data
    Exercise 2: Defining a target collection in the reschema unit
    Exercise 3: Adding and scheduling a task to create the target collection
    Exercise 4: Running an aggregate statement against the target collection
    Working with Reschema for MongoDB: Test your skills
    Reporting with Studio 3T Aggregations
    3 Topics | 1 Quiz
    Exercise 1: Creating a view based on an aggregation query
    Exercise 2: Exporting a collection as a .csv file for use by a third-party tool
    Exercise 3: Visualizing collection data in MongoDB Charts
    Reporting with Studio 3T Aggregations: Test your skills
    Course Extras
    Return to MongoDB 301: Aggregation
  • 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
    • White Papers
    • Testimonials
    • Discounts

    Company

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

    © 2023 3T Software Labs Ltd. All rights reserved.

    • Privacy Policy
    • Cookie settings
    • Impressum

    We value your privacy

    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 ("analytics and marketing") and to provide you with additional functions (“functional”). 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.

    By clicking “Accept all”, you consent to the storage of cookies and the processing of personal data for these purposes, including any transfers to third countries. By clicking on “Decline all”, you do not give your consent and we will only store cookies that are necessary for our website. You can customize the cookies we store on your device or change your selection at any time - thus also revoking your consent with effect for the future - under “Manage Cookies”, or “Cookie Settings” at the bottom of the page. You can find further information in our Privacy Policy.
    Accept all
    Decline all
    Manage cookies
    ✕

    Privacy Preference Center

    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 ("analytics and marketing") and to provide you with additional functions (“functional”). 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 our Privacy Policy.

    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.

    Google Analytics

    Google Ads

    Bing Ads

    Facebook

    LinkedIn

    Quora

    Hotjar

    Reddit

    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.

    HubSpot

    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