Use of Order by Clause in MongoDB

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

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.