MongoDB Compass: A no-code tool for data analysts
MongoDB Compass is a Graphical Interface that allows direct interaction with a MongoDB Cluster hosted on Mongo Atlas. I’m gonna show the most common queries and how you can export the result to a .csv or .json for further analysis with your favourite spreadsheet.
Note: If you’re just curious about the possible queries, download this cheat sheet.
Setting up a database of test data
The most common implementation of MongoDB is on the cloud with MongoDB Atlas. An Atlas account configured with the required permissions gives full access to real-time production data.
For this tutorial, let’s skip your tech team and use sample data from a free MongoDB Cluster. Register an account here on MongoDB Atlas.
Upon logging in, create a new project and give it a name:
Upon completion, this page shows up:
Now to “Build a Database”. Choose the “Shared” option to access the M0 sandbox which is free and limited to one per Cluster. Type a Cluster Name, use the default settings for the rest then click “Create Cluster”:
Now a page will open to set up authentication. Enter the username and password required to access the new Cluster then click “Create User”:
Now to enable your IP to access the cluster. Click “Add My Current IP Address” then click “Finish Set Up” at the bottom.
The new Cluster will take 3–5 minutes to set up. In the meantime, instruct Atlas to fill it with a sample dataset by clicking “Load Sample Dataset”:
Now to get the connection string. Click “Connect” then “Connect using MongoDB Compass”. Note that the <password> was set up earlier when creating the Cluster.
Head here to download and install MongoDB Compass.
Upon opening up the program, the “New Connection” page will open. Paste the connection string, enter the Cluster password, then click “Connect”. For faster access later, the connection string can be marked as “Favourite”.
Your first query
Any Compass query is wrapped between two curly braces with each field is separated by commas.
Let’s look at the “restaurants” Collection in the “sample_restaurants” database
If I want to know all restaurants within the borough of Queens, the query to use is:
{borough: “Queens”}
Type this into the query bar (labelled FILTER), then click “FIND”. The query results appear below it:
Now what if I also want to know all restaurants in Queens serving American cuisine? I’ll need to add a second argument:
{borough: “Queens”, cuisine: “American”}
This query basically says “Get all restaurants that are in Queens AND serve American cuisine”.
What if I want to get a single, unique Document? Each Document in MongoDB has a unique “_id” field. Search for it with the following query:
{_id: ObjectId(‘5eb3d668b31de5d588f42930’)}
“ObjectId(…)” is a special wrapper for fields with an ObjectId as its value.
Logical operators: AND, OR, NOT
In a previous example, AND is performed by doing the following:
{borough: “Queens”, cuisine: “American”}
Another way of doing the same is:
{$and:[{borough: "Queens"}, {cuisine: "American"}]}
Similarly for OR:
{$or:[{borough: “Queens”}, {cuisine: “American”}]}
NOT can take several forms. For example, to get all Documents of restaurants NOT serving “American” cuisine, use $ne:
{cuisine: {$ne:”American”}}
The opposite of this is $eq. This gets all Documents where cuisine is “American”
{cuisine: {$eq:”American”}}
I can also exclude more than 1 possible value for the same field by using $nin. For example, to exclude “Hamburgers” and “Bakery”:
{cuisine: {$nin: [“Hamburgers”,”Bakery”]}}
The opposite of $nin is $in which gets all Documents with cuisine as either “Hamburgers” or “Bakery”:
{cuisine: {$in: [“Hamburgers”,”Bakery”]}}
What if I want to check for Documents missing the “cuisine” field? This query finds it:
{cuisine:{$exists: false}}
Comparison query operators
These operators (ref) are useful when fields containing numbers and Dates are concerned. Numbers can be either in string type (“1234”) or number type (1234).
Here’s a list:
| Operation | MongoDB operator |
|-----------|------------------|
| > | $gt |
| ≥ | $gte |
| < | $lt |
| ≤ | $lte |
To try these out, let’s now move to the “movies” Collection in the “sample_mflix” database.
To get all movies with a runtime longer than 60 minutes:
{runtime: {$gt: 60}}
Note that if “runtime” is stored as a string, the query to use is:
{runtime: {$gt: "60"}}
Combining these operators allows getting all movies with a release date that fits within a range. For example, to get all that were released from 2000 to 2010:
{released: {$gt: ISODate("2000-01-01T00:00:00.000+00:00"), $lt: ISODate("2010-12-31T00:00:00.000+00:00")}}
“ISODate(…)” is a wrapper for searching fields that have Date as a value. These Date values follow the ISO 8601 format.
Note that in some DBs, UTC+0 is considered the same as Z
. Hence, the command becomes:
{released: {$gt: ISODate("2000-01-01T00:00:00.000Z"), $lt: ISODate("2010-12-31T00:00:00.000Z")}}
String query
There are 2 options with this. The first is to find an exact match:
{cuisine: “Chinese”}
Combining this with $in or $nin allows multiple exact matches in 1 query. The following gets all Documents where cuisine
!== Chinese nor American:
{cuisine: {$nin: [“Chinese”,”American”]}}
A more powerful way is with regular expressions i.e. regex. MongoDB provides the $regex operator which can be used with $options to implement regex flags.
For example, this one matches all cuisine
fields containing the string amer
:
{cuisine: {regex:/^amer/}}
While doing this matches any cuisine
field containing the string amer
anywhere in the string and in both lower and uppercase:
{cuisine: {regex:/amer/, settings:”gi”}}
Looking deeper: Dot notation
Use this to access info within objects and arrays. Think of these as a group of data within the Document.
Query objects
Let’s get movies rated 3 stars by Rotten Tomatoes in the “movies” Collection in the “sample_mflix” database. The rating is inside the “tomatoes” object along the path “tomatoes.viewer.rating”. Wrap this in double quotes then run the query:
{"tomatoes.viewer.rating": 3}
Query arrays
To get Documents of movies that have “Drama” as the first element of the “genres” array:
{"genres.0":"Drama"}
“0” gets the first element because arrays are indexed starting from zero.
However, if the index is not known, it is possible to search for all Documents with an array containing the provided value. This example returns all Documents with the array “genres” containing the string “Comedy”:
{"genres":"Comedy"}
Sometimes the data in question can be in any object in an array. Take the “grades” database in the “sample_training” Collection as an example. Using this query, it returns Documents where at least 1 object in the “scores” array has an exam score of >90:
{“scores.type”:”exam”, “scores.score”:{$gt: 90}}
Refining search results
Compass offers several tools to help refine search results. Clicking the “OPTIONS” dropdown reveals the following:
Here’s what they do:
PROJECT : Removes fields from the query result. Useful in large Collections containing Documents with many fields because it helps speed up the query by reducing the size of all returned Documents.
Set to “0” to exclude a field, “1” to include just it. For example, to exclude the “cuisine” field from all returned Documents:
To get only the “cuisine” field of each Document:
SORT: What this does depends on the target field’s data type. The value of each field for SORT can be either “1” or “-1"
- If the value is a Date, SORT arranges Documents by the earliest/latest date of the field
This query sorts comment by latest Date:
- If the value is a String, SORT arranges Documents in by the field’s string value in alphabetical order
This query sorts restaurants by name in reverse alphabetical order:
- If the value is a number, SORT arranges by increasing/decreasing number
For example, this query sorts by oldest year first:
LIMIT: Specifies the maximum number of Documents to return.
For example, this limits the number of Documents returned to 5:
SKIP: Specifies how many Documents to skip from the search result starting from the first Document.
For example, this skips the first Document from being included in the result:
Removing or setting SKIP to zero includes the first Document, which is for “Tov Kosher Kitchen” here:
MAX TIME MS: Specifies the maximum time in miliseconds to run the query. Use this to extend the query duration when querying particularly large Collections. Queries run for 60 seconds by default.
COLLATION: Allow setting language-specific rules for string comparison, such as rules for lettercase and accent marks.
Exporting search results
To export search results, click on the icon beside “ADD DATA” then select the file format to save the result. Formats available are JSON and CSV. Saving as JSON preserves the original data type of each field. The data in the export matches the query parameters specified.
Fields to include in the export can be selected on this page:
Cheat sheet
Here’s a final takeaway: I have compiled all the queries covered above into this file here:
https://github.com/YFLooi/mongodb-compass-cheat-sheet
Now go on and ask questions to your data = )