Use of Order by Clause in MongoDB syntax

select(find) query with Order by (sort) in Mongodb

Example show you the Select query from RDBMS database fetching the table data in ascending order of column name.

SELECT * FROM employees ORDER BY name ASC

— In MongoDB we can fetch ascending order of collection by using SORT function as same:
db.employees.find().sort( {name:1} )

Example show you fetch data in desc order by RDBMS and MongoDB query:

-- In SQL RDBMS fetching data in desc order.
select * from employees order by name desc

— In MongoDB fetching data in descending order.
db.employees.find().sort( {name:-1} )

Leave a Reply