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

Lesson 7, Exercise 1: Creating a MongoDB view

MongoDB 201: Querying MongoDB Data Working with MongoDB Views Lesson 7, Exercise 1: Creating a MongoDB view

In this exercise, you’ll use the View Editor in Studio 3T to create a MongoDB view that retrieves data from the customers collection.

The view will aggregate data based on the package and prio_support fields, providing the total number of transactions for each group.

The exercises in this section use 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 or if you’ve deleted them since reviewing the first section.

To create the view

1. Launch Studio 3T and connect to MongoDB Atlas.

Don’t have a MongoDB Atlas instance? Here’s how to set up a free cluster, or connect to an existing MongoDB database or localhost instead.

2. In the Connection Tree, expand the sales database node and, if necessary, expand the Collections node for that database.

3. Right-click the customers collection node and click Add View Here.

Studio 3T launches the View Editor in its own tab. The View Editor is nearly identical to the Aggregation Editor, except that it provides an option for creating a view, as shown in the following figure. The option is near the top left corner, outlined by the red rectangle in the figure.

create view

At this point, you can build your own aggregation pipeline from scratch, defining each stage as needed, just as you would in the Aggregation Editor.

However, because aggregations were covered earlier in this course, you’ll take an easier approach this time around and create your view based on a pre-defined aggregate statement.

Knowing how to create a view from an existing aggregate statement can be a useful tool to have at your disposal. It provides a simple, straightforward method for building a view without having to redefine the entire aggregation pipeline.

4. Copy the following aggregate statement to your clipboard:

db.getCollection("customers").aggregate(
  [
    {
      "$match" : { 
        "transactions" : { 
          "$gt" : NumberLong(0) } }
    }, 
    {
      "$group" : { 
        "_id" : { 
          "package" : "$package", 
          "prio_support" : "$prio_support" }, 
        "SUM(transactions)" : { 
          "$sum" : "$transactions" } }
    }, 
    { 
      "$project" : { 
        "package" : "$_id.package", 
        "prio_support" : "$_id.prio_support", 
        "transactions" : "$SUM(transactions)", 
        "_id" : NumberInt(0) }
    }, 
    { 
      "$sort" : { 
        "package" : NumberInt(1), 
        "prio_support" : NumberInt(1) }
    }
  ]
);

The statement’s aggregation pipeline defines the following four stages:

  • The first stage is based on the $match operator. It limits the documents to those with a transactions value greater than 0. This helps eliminate any anomalies in the data, such as outliers that don’t include the transactions field.
  • The second stage is based on the $group operator. It groups the documents first by the package field and then by the prio_support field. It then calculates the total number of transactions for each group.
  • The third stage is based on the $project operator. It defines three fields to be included in the results, while excluding the _id field.
  • The fourth stage is based on the $sort operator. It sorts the documents first by the package field and then by the prio_support field, both in ascending order.

5. In the View Editor, click the paste button in the upper right corner (Paste from clipboard icon). It’s the button with the tooltip that reads Pastes an aggregate query from the clipboard.

Clicking the button automatically populates the Pipeline tab with the four aggregation stages from your aggregate statement, as shown in the following figure.

6. Press F5 to run the pipeline stages. It’s always a good idea to run the query at this point to ensure it will return the expected results once you save the aggregation as a view.

Studio 3T should return 15 rows, as shown in the following figure.

Pipeline output

7. Click the Create View button on the View Editor toolbar.

8. In the Create View dialog box, type package_totals in the Enter view name text box, and then click OK.

Studio 3T adds the view beneath the Views node in the Connection Tree and opens the view results in its own tab, which is similar to a collection tab. The following figure shows the package_totals view tab that launches after creating the view.

result

A view tab works much like a collection tab. You can query and sort documents, launch Visual Query Builder, select different views, and carry out a number of other tasks. However, you can only read the data. You cannot update it like you can on a collection tab.

9. Close the View Editor, but leave the package_totals view tab open for the next exercise.

You can also create a view from the Aggregation Editor, which can be useful if you start building an aggregation query and then decide to save it as a view. This capability also lets you run an SQL aggregation statement in SQL Query, open the resulting mongo shell code in the Aggregation Editor, and then save the aggregation as a view.

Previous Lesson
Back to Lesson
Next Topic
  • Course Home Expand All
    Performing MongoDB CRUD Operations
    4 Topics | 1 Quiz
    Lesson 1, Exercise 1: Adding a document to a collection
    Lesson 1, Exercise 2: Viewing a document in a collection
    Lesson 1, Exercise 3: Updating a document in a collection
    Lesson 1, Exercise 4: Deleting a document from a collection
    Test your skills: Performing CRUD Operations
    Building MongoDB find() Queries
    4 Topics | 1 Quiz
    Lesson 2: The MongoDB find method
    Lesson 2, Exercise 1: Using IntelliShell to build and run find statements
    Lesson 2, Exercise 2: Using Visual Query Builder to build and run find statements
    Lesson 2, Exercise 3: Using Query Code and IntelliShell to modify and run a find statement
    Test your skills: Building MongoDB find() Queries
    Working with the MongoDB Aggregation Pipeline
    6 Topics | 1 Quiz
    Lesson 3: Introducing the MongoDB aggregate method
    Lesson 3, Exercise 1: Filtering the documents in the aggregation pipeline
    Lesson 3, Exercise 2: Grouping the documents in the aggregation pipeline
    Lesson 3, Exercise 3: Adding and removing fields in the aggregation pipeline
    Lesson 3, Exercise 4: Changing the field order in the aggregation pipeline
    Lesson 3, Exercise 5: Sorting the documents in the aggregation pipeline
    Test your skills: Working with the MongoDB Aggregation Pipeline
    Querying Arrays Using MongoDB $elemMatch
    4 Topics | 1 Quiz
    Lesson 4, Exercise 1: Using IntelliShell to query single and multiple values in an array
    Lesson 4, Exercise 2: Using Visual Query Builder to query a single array value
    Lesson 4, Exercise 3: Using Visual Query Builder to query multiple array values
    Test your skills: Querying Arrays Using MongoDB $elemMatch
    MongoDB 201 Mid-Course Feedback
    Querying Embedded Documents in MongoDB Arrays
    3 Topics | 1 Quiz
    Lesson 5, Exercise 1: Using the $elemMatch operator to query embedded documents
    Lesson 5, Exercise 2: Using conditional operators to query embedded documents
    Lesson 5, Exercise 3: Using Visual Query Builder to query embedded documents
    Test your skills: Querying Embedded Documents in Arrays
    Querying MongoDB with SQL SELECT Statements
    2 Topics | 1 Quiz
    Lesson 6, Exercise 1: Using the SQL Query tool to run SQL statements
    Lesson 6, Exercise 2: Using the SQL Query tool to aggregate collection data
    Test your skills: Querying MongoDB with SQL
    Working with MongoDB Views
    3 Topics | 1 Quiz
    Lesson 7, Exercise 1: Creating a MongoDB view
    Lesson 7, Exercise 2: Querying a MongoDB view
    Lesson 7, Exercise 3: Modifying and deleting a MongoDB view
    Test your skills: Working with MongoDB Views
    Course Extras
    Return to MongoDB 201: Querying MongoDB Data
  • 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

    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