Sometimes, while getting data from the database, we may need to sort it, or get a certain number of records, or skip some records. In this case, MongoDB has the following methods: sort(), limit(), and skip(). In this topic, we will talk about them and their implementation in development.
The sort() method
The sort() method in MongoDB is used to sort the records by some field. The syntax of this method is the following:
.sort({field: 1 | -1})
The sorting order of the result set is specified by the field: pair of values in the argument. An ascending or descending sort is defined by 1 or -1, accordingly.
Sorting can be applied to the find() method. In this case, the output data will be sorted in the specified order. Now let's look at some examples. We have a collection called users where information about the name, age and premium subscription is stored:
[
{id: 1, name: "John", age: 25, subscription: true},
{id: 2, name: "Lisa", age: 18, subscription: false},
{id: 3, name: "Mark", age: 23, subscription: false},
{id: 4, name: "Alisa", age: 28, subscription: true}
]
Now we will sort the output records about these people in ascending order of their age:
db.users.find().sort({age: 1})
We get this as a result:
[
{id: 2, name: "Lisa", age: 18, subscription: false},
{id: 3, name: "Mark", age: 23, subscription: false},
{id: 1, name: "John", age: 25, subscription: true},
{id: 4, name: "Alisa", age: 28, subscription: true}
]
The ages are sorted from the youngest to the oldest, as we expected.
If we will use -1 as a field value in parameter, the result will be the opposite:
[
{id: 4, name: "Alisa", age: 28, subscription: true},
{id: 1, name: "John", age: 25, subscription: true},
{id: 3, name: "Mark", age: 23, subscription: false},
{id: 2, name: "Lisa", age: 18, subscription: false}
]
The sorting order also depends on the type of field by which sorting takes place. You may perform sorting operations on a variety of data formats such as string, integer, decimal, and so on. For example, when sorting strings, MongoDB by default uses a straightforward binary comparison. You can also create your own rules for string comparison and sorting using Collation. You can get more information about sorting of other data types in MongoDB documentation.
Sorting by multiple fields
It is also possible to set multiple fields in the sort() method. Then the sorting will take into account each field in the order in which they are specified. First, sorting will take place by the first field. However, if there are several records in the collection with the specified value, the method will sort them by the second field specified by us.
Below we will add a few records with the same name field but different age value to see how it works.
[
{id: 1, name: "John", age: 25, subscription: true},
{id: 2, name: "Lisa", age: 18, subscription: false},
{id: 3, name: "Mark", age: 23, subscription: false},
{id: 4, name: "Alisa", age: 28, subscription: true},
{id: 5, name: "John", age: 31, subscription: true},
{id: 6, name: "John", age: 22, subscription: true}
]
Now we have two more documents about John. Let's sort our new collection by two fields: name and age.
db.users.find().sort({name: 1, age: 1})
The result will be the following:
[
{id: 4, name: "Alisa", age: 28, subscription: true},
{id: 6, name: "John", age: 22, subscription: true},
{id: 1, name: "John", age: 25, subscription: true},
{id: 5, name: "John", age: 31, subscription: true},
{id: 2, name: "Lisa", age: 18, subscription: false},
{id: 3, name: "Mark", age: 23, subscription: false}
]
As you can see, we have got names sorted in alphabetical order, and the ages of three users with the same name are sorted in ascending order. Everything is exactly as we expected. You can add many fields to the argument, and the sorting process will be the same.
Now that you have an idea about how the sort() method works, it is time for the next one.
The limit() method
The limit() method limits the number of output records from the collection. We use this method with the find() to limit the records we want to get. The limit() syntax is such:
.limit(<number>)
The argument of this method shows the number of documents we want to get at the output. If you pass 0 as an argument, there will be no limit at all.
Let's take a look at the examples. The same collection, the same data as in the previous section. Let's get the first two records from our users collection:
db.users.find().limit(2)
And we will get the first two documents from the collection:
[
{id: 1, name: "John", age: 25, subscription: true},
{id: 2, name: "Lisa", age: 18, subscription: false}
]
Now let's make our condition a little more complex by adding some other criteria. We will get only the first user with an active subscription:
db.users.find({subscription: true}).limit(1)
In the same way, we can get as many records from the collection as we want and insert as many criteria as we want. Notice that if the limit number is greater than the number of records in the collection, it will just output all records.
The skip() method
We can say that the skip() method is the opposite of the limit() method. It skips the number of records that we specify. It has the same syntax:
.skip(<number>)
The argument can take any positive number. The 0 as an argument will mean no skipped data.
We will skip the first three records from our users collection:
db.users.find().skip(3)
And we will get only one record, the last one:
[
{id: 4, name: "Alisa", age: 28, subscription: true}
]
The skip() method was pretty simple, right? Of course, you can use all these methods with more complicated find() conditions. We have just discussed some basic examples to understand the main concepts.
Conclusion
In this topic, you have learned about three more methods in MongoDB: sort(), limit() and skip().
- Use the
sort()if you want to sort the data. You can sort the data by multiple fields. - Use the
limit()if you want to output a certain amount of data. - Use the
skip()if you want to skip a certain number of records.
Now it's time to practice! Then you will be 100% ready to use these methods in your projects!