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

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

How to Create a MongoDB View

Posted on: 14/02/2020 (last updated: 09/03/2022) by Kathryn Vargas

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.

Download the latest Studio 3T version and let’s begin.

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.


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.


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

  • Exercise 2: Unwinding an array to create individual documents
  • Exercise 3: Adding and scheduling a task to create the target collection
  • 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

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