Sort() Method in MongoDB

Topics Covered

Overview

The sort() method in MongoDB is used to provide the ordering for the documents returned by the query as a result. This method can be applied before fetching the documents from the collection. Sort in mongoDB takes the document as a parameter which has the field: value, here field specifies the name of the field based on which ordering of documents is performed and the value specifies the sorting order. Value can take 1 or -1 value, 1 means ascending and -1 means descending order.

Introduction to $sort in MongoDB

All the input documents are sorted by the $sort in MongoDB, it takes the document as an argument for specifying the sorting field and order of sorting. Sorting field and order is provided in the form of field : order and we can specify 1 for ascending order and -1 for descending order.

Behavior

Limits: Sorting can be performed on 32 maximum keys.

Sort Consistency: MongoDB does not maintain any particular order for storing documents in a collection. When the sorting is performed in documents having duplicate values, then those documents having duplicate values can be returned in any order. If you want consistent sorting then for sorting at least one field having all unique values must be provided. And we can easily achieve this by using the _id field for the document sorting.

Syntax

Parameters

<field>: <sort order> pair in the form of a document is passed as a parameter where the field specifies the name of the field and we can pass 1 or -1 as a sort order, and the sort order specifies the order of sorting. 1 means ascending order for sorting and 0 means descending sorting order.

Return values

Queried documents are returned by it in sorted order.

Examples

Here we are taken an example of the Student collection, so we create a collection and insert some documents into it.

student collection database

  • Now fetching the student in ascending order of the age.

Query:

Output:

output of student database

  • Fetching the student in descending order of the age.

Query:

Output:

output of student database sort

  • Sorting the student in ascending order of age and sort in ascending order of name if two have the same age.

Query:

Output:

sort on age and name

  • Fetching the student in ascending order of the name.

Query:

Output:

sort on name

  • Fetching the student in descending order of the name.

Query:

Output:

student datebase sort on name

FAQs

Q: Does the original stored order of the documents modified by the sort in MongoDB?

A No, the order of original documents of the collection is not modified by the sort(). It only returns the sorted order of the documents as the result of the query.

Q: What is the sort in MongoDB?

A Sort in MongoDB is used for sorting the documents of the collection based on specified fields and sorting order. It returns the query result in the sorted form of documents.

Q: How MongoDB sort() method can be used?

A: For using the MongoDB sort() method, we need to pass the fields to be used for sorting and sorting order in the form of field: sorting_value within the document.

Syntax: db.collectionName.sort({<field1>: <sort order>, <field2>: <sort order> ...})

Conclusion

  • The sort() method in MongoDB is used to provide the ordering for the documents returned by the query as a result.
  • Sort in MongoDB takes the document as an argument for specifying the sorting column and order of sorting.
  • Sorted documents are returned by sort in MongoDB.
  • Order of original documents of the collection is not modified by the sort().