Try Studio 3T for Free

How to Create a MongoDB View

Check out our introduction to MongoDB views here.

In SQL databases, a view lets you pre-specify a query so that you can quickly view its results.

Think of it as a virtual shortcut – an additional layer that doesn’t take up disk space and isn’t part of your data’s schema – to view specific information through this predetermined query.

MongoDB makes it possible to create views through the db.createView() method, which works by applying a specified aggregation pipeline to a collection.

Our MongoDB GUI, Studio 3T, makes creating a view even more straightforward through a built-in View Editor.

And if you’re still learning JSON query syntax, you can still create a view without knowing the MongoDB query language using our Query Code feature.

Create a MongoDB view

1. In the Connection Tree, right-click on the collection to which you want to add a view.

2. Click Add View Here. This will open the View Editor where you can build your query with the desired operators and add stages accordingly.

In Studio 3T, right-click on any connection and choose Add View Here to add a view

3. Click on the (+) button to add a new stage.

4. Choose your desired operator from the dropdown. Repeat steps 3 and 4 as needed.

5. Once done, click Create View to save.Studio 3T's View Editor makes it easy to add a view

Create a MongoDB view from a SQL Query

If you’re using the SQL language to query MongoDB (through our SQL Query feature), you can still create the familiar SQL view in two ways, depending on whether you have a simple or complex SQL query.

Simple SQL queries

You can get the mongo shell translation of simple SQL queries through Query Code, which you can then segment and add as a $match stage and other relevant stages while in View Editor.

In the example below, we’ve written a SQL query to select only customers with the last name Miller and show only the specific fields _id, first, last, and address.state:

An example of a simple query written in SQL to query MongoDB

Through the Query Code tab, we can get the translation of this SQL query to a MongoDB query:

Following the steps above, you can divide your MongoDB query into the appropriate stages (in this case, $match and $project), and add them as stages in View Editor:

Here, we've added the $match and $project stages in Studio 3T's View Editor

(Not-so) simple SQL queries

For more complex SQL queries which include GROUP BY, ORDER BY, and other aggregate functions like COUNT, MIN, MAX, SUM and AVG, Query Code automatically generates a MongoDB aggregation query translation, which you can directly copy and paste in View Editor.

Below, we’ve written a SQL query which uses the COUNT and GROUP BY function:

SQL query with a COUNT aggregate function

Query Code detects that a SQL aggregation function has been used, so it automatically generates the equivalent MongoDB aggregation query:

MongoDB aggregation query translated and generated from Query Code

From here, simply copy the MongoDB aggregation query by clicking on the Copy button:

Add a view following steps 1 and 2 above, then paste your MongoDB aggregation query by clicking on the Paste button:

The View Editor automatically splits your pasted aggregation query into stages, so all you need to do is click on Create View:

Click on Create View while in View Editor

Create a MongoDB view from Aggregation Editor

You can create a view directly from an aggregation query built through the Aggregation Editor.

Build an aggregation query (like this example), right-click anywhere in the Pipeline and Stage tabs, and choose Create view from this aggregation query.

Create a view from an aggregation query

Name the view and click OK.

Create view from aggregation query

Your view should appear in the Connection Tree, under the database where your collection is located, within a separate folder called Views.

New view should appear in the Connection Tree

Complement this guide with our how-to on comparing MongoDB collections, or expand your MongoDB knowledge with our tutorials on how to create an index strategy and investigate MongoDB query performance.

Updated on December 7, 2020

Was this article helpful?

Related Articles


  1. Hi Thank’s for your help. I’ve a question. When data become update , what will happen on view? When we read data from view, each time compute view from the beginning or update with new inserted data?

    1. Hello!

      Just to clarify, each MongoDB view has its own aggregation pipeline against the source data. MongoDB calculates the view contents by automatically executing the aggregation during read operations. It also executes read operations on views as part of the underlying pipeline.

      Hope this helps!

Leave a Comment