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.

Log in

Log in to your Studio 3T Academy account

Forgot password?

Don't have an account yet? Sign up
Sign up

Start learning MongoDB today. All fields are mandatory.

By signing up for a course, you agree to the 3T Software Labs Privacy Policy.


Already have an account? Log in