Sorting Records on MongoDB Query

Sorting Records on MongoDB Query

Instructor-svgAl-Mamun Sarkar
Sep 11 , 2020

In this lesson, I will show you how you can sort records in ascending and descending order on MongoDB SELECT queries.

Sorting On Records:

Syntax:

db.collection.find().sort({key:1})
db.collection.find().sort({key:-1})

The value of the key will be 1 for ascending order and -1 for descending order.

 

Example:

Sort in Ascending Order:

db.employees.find().sort({salary: 1})

Sort In Descending Order:

db.employees.find().sort({salary: -1})

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram