Home » Node.js MongoDB Sorting

Node.js MongoDB Sorting

by Online Tutorials Library

Node.js MongoDB Sorting

In MongoDB, the sort() method is used for sorting the results in ascending or descending order. The sort() method uses a parameter to define the object sorting order.


Sort in Ascending Order

Example

Sort the records in ascending order by the name.

Create a js file named “sortasc.js”, having the following code:

Open the command terminal and run the following command:

Node.js Sorting 1


Sort in Descending Order

Example

Sort the records in descending order according to name:

Create a js file named “sortdsc.js”, having the following code:

Open the command terminal and run the following command:

Node.js Sorting 2


You may also like