A Detailed insight of Query Modification in MongoDB using Sort().
Cursor and its methods within MongoDB were explained in detail in our previous tutorial in this MongoDB Training series.
In this tutorial, we will take an in-depth look at the Sort method in MongoDB.
Sort method is used to prioritize documents or arranges their order within a mongo collection. In simple words, the sort method arranges all the documents which are matching specific criteria and returns the results.
As you know, a record is returned in the form of a cursor and sort method is applied on the cursor. Thus when we combine sort method with a cursor, it will return us the documents in a specific order.
Sort method has only one parameter in which we have to pass the field and their value. Parameters are passed in JSON Format like {Field: Value} within parenthesis of the sort method. We can also pass multiple fields and values to sort records on them.
Table of Contents:
Ascending & Descending Sort
This method allows us to sort documents in an ascending and descending order. If we pass “1” as a parameter of the sort field, then it will arrange the documents in ascending order. If we pass “-1” as a parameter of the field, then it will return all the records in descending order.
Syntax
db.collection.find().sort({field:order})
Code
db.staff.find().sort({staff_id:1}) db.staff.find().sort({staff_id:-1})
Figure 1: In Mongo Shell
Figure 2: In Robo 3T
We can also sort multiple fields at the same time. You can observe this scenario with the help of the following code.
Syntax
db.collection.find().sort({field:order, field:order })
Sorting Limitations
Mongo DB sorts all the records in the virtual memory and there is a condition which is applied on data-set. The condition is that the records should be less than 32 megabytes. Sometimes sorting is not performed due to high memory usage.
Suppose if you have a large group of documents that are more than 32 MB memory, then it will not perform any sorting.
Instead, it will return an error which becomes the cause to eliminate this query. In order to optimize this situation either you can use a combination of limit and sort together or you can use proper indexing.
Limiting Sort Results
We can use the sort method along with the limit method to get the limited and sorted record together. In this way, we can optimize query operations on Mongo DB and also increase the speed along with memory consumption.
We can also use the top-k sort algorithm to optimize our queries. In this algorithm, whenever the records consume memory more than 32MB, then the query will automatically be eliminated.
Conclusion
Sort method helps us to arrange the record in a specific order. We can get documents in ascending and descending order. We can also apply multiple ordering with the help of sort method in different fields.
In the case of alphabetic values of the field, sorting is applied by alphabetic order through “1” and “-1”. Such as one will arrange all the records from A to Z and negative one will arrange the record from Z to A.
Stay tuned to our upcoming tutorial to know more about MongoDB Performance!!