MongoDB Compass is the official database viewer and query analyzer for the Mongo database. It is a freely available graphic interface tool and provides a lot of interesting as well as useful functionalities to the users. In this blog, we will explore the several use cases of MongoDB Compass and determine how useful they can be in terms of viewing our database and collections.
For the installation of MongoDB Compass visit:
https://www.mongodb.com/download-center/compass
Pick your platform or simply operating system (OS) first and start downloading. Once downloaded, the time has come to see what it carries with it. So open the Compass, and this is the first screen you can see.
So now, let’s understand this page bit by bit.
Left Side Part
- New connection: It is used to establish a new connection.
- Favorite: In this, you get the database list you have marked as favorites, and you can quickly find them.
Middle Part
- Mongo URI provides a sophisticated way to connect to the database. In other words, you can directly establish a Mongo connection. It doesn’t matter whether it is a local connection or server-based connection.
- We are going to follow a local host strategy, but it’s also very easy to connect to the server if you provide accurate details.
Right Side Part
- You can create a free cluster on MongoDB Atlas, which is an online site. In simple words, you can host your database and cluster (fancy word for an instance of a database) for free.
Now, let’s connect to our local database.
Now see, we have used a connection string for making our connection to local Mongo. 27017 is the default port on which mongo database runs. However, in case of any confusion, you can click on this text Fill in connection fields individually.
- Hostname: 127.0.0.1 is used when we have to connect to Mongo on local.
- Port: We have already mentioned that 27017 this default Mongo port.
- Authentication set: None, as we are not using authentication for connection to a local database, but database hosting the server usually requires these based on their security type.
Again nothing very fancy, we can see previously created databases on the left side. In the middle, we can explore some extra details like the number of collections and indexes for that database. Using an option to create a database that is on top, feel free to create a new database.
Let’s now search one database and see what features Compass came up with to enhance our experience. You can open one collection and see how it works.
Documents Tab
Now, with respect to the above image and documents page rather than writing raw query (which as per me one should have knowledge of ), we can use particular JSON in each category like in
- Filter: It provides desired fields in the collection.
- Projection: It determines which fields must be displayed in documents after the successful execution of a query.
- Sort: It performs sorting by a field and its value.
- MAXTIMEMS: Now, this is a very interesting functionality. What happens many times when running a query mongo gives shell timeout error, so from here, I can increase the overall wait for the query to return data before giving shell timeout error.
- SKIP and LIMIT: Both are simple options for skipping and limiting the number of documents per query.
We can see already executed queries on a particular collection, and the time at which we ran the query, and we can simply bookmark them on the right side. So it can be run later on, and we don’t have to write everything again, we can simply click on the stored query object now to know that these query in the right tab will be shown as per collections.
Adding Data In Collections
There are two ways to add data in a collection in Compass.
First Step
- As you can see above, select a file either JSON or CSV whichever preferred, then select the input file type and import that particular file and make new collections.
- Some options are provided, which can give ignore empty string and Stop on errors. Both are tests for data imports, which ensures no unwanted or bad data is applied to the collection.
Second Step
- Now, this is one of the traditional approaches for adding data, keys, values, and selecting your datatype or adding everything in raw format.
Exporting Data
It is essential for creating backups and maintaining a copy of important collections. In general, one should know how to import and export the database. As it comes very handy in many situations. In Compass, there are again two features.
First Step
- You may also add the necessary fields, either export the entire collection or specific documents with a query, which is a beneficial function.
- This screen will enable us to add desired fields per document, and in the next screen select in which file you want to export the collection. To put it simply, first select any file JSON or CSV and output the collection back in that file. This will create a file which contains that collection details.
Second Step
- Add a query to select only particular data.
Aggregation Tab
Now let’s switch to the aggregation tab; I know that this looks weird, and there is the use of word pipeline in this, but in case you don’t know. I will try to simplify the word pipeline with regards to Mongo. The pipeline composed of stages or steps which we apply to our collection through aggregation function provided by Mongo. It also provides a lot of operators or functions, which we use to perform a query on our collection. Let me show you one.
So now what I did in above image.
- Added one stage in the aggregation pipeline and selected an operator count.
- Provided it a key type.
- It gives results 5.
Let me explain why it gives this result; it checks the key type of the document of collection and then counts the number of documents in which key name type was present. Similarly, you can group documents or sum them based on some condition.
There are a few more things that can be performed on the Aggregation tab.
- Topmost of them is to save you aggregation pipeline code or work in a file so it can be used later.
- There are options to Limit and Max Time, which are there to limit your number of documents return my aggregation cause if a user uses groupby operator or other operator listed and if it exceeds certain memory and rams the pipeline stage will break so another way to make sure that our result is specific but one can undoubtedly alter them based on needs.
Schema Tab
This is very useful in terms of seeing values in the collection and data types of the document key.
- If you see below the active key is it's a data type, in our case it is boolean. On the right side, you can see a bar which gives used values in graphical form.
- You can also see how much each value impacts the present key means true and false.
- If you see closely, you can see that true proportion is greater than false means in this collection every (which you can also add a condition for a document as filter tab is there on top) document key active hold's more true value than false.
Now, different data types have different graphical representations, as you can see in the above image like date, etc.
In general, I think this information is useful for knowing and analyzing how a collection is working.
Explain Plan Tab
This tab is there to check the response of our query, which is written in the above screenshot as it tells how much time a query takes to execute and return data, and also, there is data regarding the index in this.
Index Tab
Now in this, one can see what are all indexes related to the collection, what are their properties, and the size of an index. Index can delete an index too.
Note: Primary key index cannot be deleted.
Create Index
This will be used to add an index name, adding a field name, and selecting the index sorting with either -1 or 1.
For options list, click on the drop-down, and it will open a list click on a small icon provide with the text, and it will explain each option in detail.
Validation Tab
Now, this is the final tab that tells validation over each document. This means we write some validation action, and it will run over the whole collection and schema and check if any documents failed over that set of validation rules means.
See, this has added a required field for email, and for active added a property that value is an enum(choice-based) that it can be either true or false, so with this validate, no document failed our validation check.
Note: if something passes or fails, it will only return a sample of that document present in our collection, not all of the documents.
Now, if I remove false from enum choice, you can see that there is one document that failed our validation check.
So with this now, we know all the features available in the mongo Compass.