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

Querying MongoDB with SQL SELECT Statements

MongoDB 201: Querying MongoDB Data Querying MongoDB with SQL SELECT Statements

Developers and administrators often come to MongoDB with a background in relational database management systems such as SQL Server or Oracle Database.

These systems rely on the Structured Query Language (SQL) for accessing and manipulating data.

If you’re one of these people, you’ll be happy to know that Studio 3T includes the SQL Query tool, which lets you run SQL SELECT statements directly against a MongoDB collection, using the same syntax you use for a relational database. 

In this section, you’ll learn how to use the SQL Query tool to run different types of SQL SELECT statements.

You’ll also see how the tool translates a SELECT statement to a mongo shell find statement or aggregate statement, depending on whether the SELECT statement groups and aggregates data.

In addition, you’ll learn how to export an aggregate statement to the Aggregation Editor in Studio 3T, where you can modify the query to meet your needs. 

The acronym SQL, when used to refer to the query language, is officially pronounced S-Q-L, rather than sequel, which is how it’s pronounced in products such as SQL Server. This is why you’ll often see a statement or other element referred to as an SQL statement, rather than a SQL statement.

Running SQL statements against a MongoDB collection

An SQL SELECT statement typically retrieves data from tables in a database, much like a mongo shell find statement retrieves documents from a collection in a MongoDB database.

When you run a SELECT statement in SQL Query, the tool automatically translates the statement to a mongo shell find statement or aggregate statement and runs it against the target collection.

If you’re well-versed in SQL, the SQL Query tool offers a quick and easy way to create these statements even if you’re new to MongoDB. The tool can also serve as a useful learning aid for familiarizing yourself with the mongo shell language and understanding how to search data in a MongoDB collection.

For example, suppose you want to retrieve all documents in the customers collection (which you created at the beginning of this course). If you’re using the mongo shell to retrieve the data, you can run the following find statement:

db.getCollection("customers").find({});

The statement calls the find method on the customers collection without passing any arguments into the method. Consequently, the statement will return all documents in the collection. You can achieve the same results in SQL Query by running the following SELECT statement:

select *
from customers;

The statement includes two SQL clauses: SELECT and FROM.

  • The SELECT clause includes only the SELECT keyword and the asterisk (*) wildcard, which indicates that all columns (fields) should be returned.
  • The FROM clause identifies the target data source, in this case, the customers collection.

If you were querying a relational database, you would specify a table name, but the collection name replaces that, allowing you to retrieve data from a collection.

The following table shows how SQL components map to MongoDB document components.

SQLMongoDB
databasedatabase
table collection 
columnfield
row (record)document

Related tutorial: Comparing MongoDB vs SQL Concepts

Not surprisingly, SQL Query lets you run more complex SELECT statements than the simple one you just saw. For example, the following statement refines the query by limiting the fields and documents included in the results:

select first, last, transactions
from customers
where transactions > 65
order by transactions desc, last asc;

As in the preceding example, the statement starts with the SELECT clause, but this time, it specifies that only the first, last, and transactions fields should be returned.

The statement also includes a WHERE clause, which limits the results to documents with a transactions value greater than 65.

Finally, the ORDER BY clause sorts the results, first by the transactions values, in descending order, and then by the last values, in ascending order.

If you were to run this statement and then view the Query Code tab in SQL Query, you would see the statement translated to the mongo shell find statement shown in the following figure:

Label query
How sections of a MongoDB query translate to their equivalent SQL SELECT statement clauses

The figure also indicates how each section of the mongo shell find statement maps to the original SQL SELECT statement clauses:

  • The find method call on the customers collection maps to the SELECT statement’s FROM clause.
  • The find statement’s query section maps to the SELECT statement’s WHERE clause.
  • The find statement’s projection section maps to the SELECT statement’s SELECT clause.
  • The find statement’s sort section maps to the SELECT statement’s ORDER BY clause.

After SQL Query has generated the find statement, you can then copy it for other applications or open it in IntelliShell, where you can then modify it further.

If your original SELECT statement includes an aggregation, SQL Query will instead generate a mongo shell aggregate statement, which you can then edit in the Aggregation Editor or in IntelliShell.

SQL Query can also convert an SQL statement to JavaScript, Java, C#, Python, PHP, or Ruby. You need only select the applicable option from the Language drop-down list near the top of the Query Code tab.

SQL Query supports many of the basic elements that make up an SQL statement. For example, as the previous example demonstrates, you can include SELECT, FROM, WHERE, and ORDER BY clauses.

You can also include GROUP BY and HAVING clauses, as well as comparison operators, logical operators, built-in functions, and wildcards.

SQL Query also supports SQL joins within the context of a collection. Although we can’t cover all these elements within a single section, you can find more information about which ones are supported and how they work in the Studio 3T Knowledge Base article, SQL Query.

By the end of this section, you will learn how to:

  • Use the SQL Query tool to run SQL statements
  • Use the SQL Query tool to aggregate collection data

What you will need:

  • Access to a MongoDB Atlas cluster
  • Access to the customers collection in the sales database
Lesson Content
0% Complete 0/2 Steps
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
Previous Lesson
Back to Course
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