How to Sort in MongoDB Compass Community
Introduction
If you’re looking for an easy way to visualize and understand your data, MongoDB Compass Community is a simple solution. MongoDB Compass acts as a GUI for MongoDB, allowing you to navigate through your databases, execute queries and perform various operations. Common database tasks, such as sorting the documents in a collection, can be accomplished with just a few clicks. In this tutorial, we’ll provide step-by-step instructions for sorting in MongoDB Compass Community.
Prerequisites
Before we turn our attention to the MongoDB Compass Community interface, it’s important to review the various prerequisites for the task. The system requirements include the following:
First, you need to confirm that the MongoDB service is running.
You’ll need to have a version of MongoDB Compass installed. For this tutorial, we’ll be using Compass Community Edition, which is a free version of the Compass product.
Finally, you’ll need to make sure the MongoDB Compass application is working before proceeding with the tutorial.
NOTE: Throughout this tutorial, the MongoDB Compass Community version being used is 1.18.0.
Start MongoDB Compass Community
Now that we’ve covered all the system requirements, let’s begin by opening up the MongoDB Compass application. Once the application finishes loading up the necessary files, you’ll be prompted with a window for the hostname and port number. In this tutorial, we’ll be working on localhost and running MongoDB on the default port 27017, so we’ll accept all the defaults and just hit “Connect”. However, you may need to enter a different host and port depending on your individual requirements.
NOTE Should you encounter an error stating something along the lines of "MongoDB not running on the provided host and port"
, check to see if the MongoDB server is running in the background.
Once MongoDB Compass connects to the MongoDB service, it will prompt a window showing all the databases that are available in that particular connection.
Sorting a MongoDB Collection
As we mentioned above, the left pane of the MongoDB Compass interface lists all the stored databases in the MongoDB server. The next step will be to select the database you’d like to access, and MongoDB Compass will then list all collections that are associated with the selected database.
In this example, you can see we have four collections associated with this database.
After selecting the desired collection, click on the Documents tab
Click the OPTIONS drop-down list to show other MongoDB Compass options. You can choose from a number of options, such as FILTER, COLLATION and SORT. In this tutorial, we’ll be focusing on sorting.
After selecting SORT, choose the field that you’d like to sort on. In this case, we’ll be using the field position
to sort all documents within the collection. To do this, type in "{position:-1}"
in the ()SORT
field. then click FIND to perform the operation. Specifying the "-1"
indicates that we want to sort in descending order.
We can see the differences in the data below, before and after running the “sort operation”.
Here’s the data before applying the sort operation:
After applying the sort operation, you can see that the documents are now sorted by "position"
in descending order.
As you can see, the code {position:-1}
that we described above simply performs a sorting of documents in a descending order.
Conclusion
The MongoDB Compass interface provides an intuitive GUI to help you explore your data and perform common database operations. There are many MongoDB Compass options to choose from: Whether you want to query data, sort a collection or optimize performance, it’s easy to perform these tasks with just a few clicks. In our example today, we reviewed the simple process of sorting in MongoDB Compass Community. With these step-by-step instructions, you’ll have no trouble sorting any of your collections or query results using MongoDB Compass.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started