Read Time:2 Minute, 26 Second
MongoDB provides the db.collection.find()
method returns a cursor object for the resulted documents. Use the cursor.sort()
method or db.collection.find().sort()
to sort the resulted documents in a cursor based on the specified order.
Syntax:
db.collection.find().sort(document)
Parameters:
- document: A document that defines the sort order in
{field: 1, field:-1,...}
format where 1 is for ascending order and -1 is for descending order.
The following inserts documents in the employees
collection.
Sample Data
db.employees.insertMany([ { _id:1, firstName: "John", lastName: "King", email: "john.king@abc.com", salary: 5000, skills: [ "Angular", "React", "MongoDB" ], department: { "name":"IT" } }, { _id:2, firstName: "Sachin", lastName: "T", email: "sachin.t@abc.com", salary: 8000, skills: [ "Accounting", "Tax" ], department: { "name":"Finance" } }, { _id:3, firstName: "James", lastName: "Bond", email: "jamesb@abc.com", salary: 7500, skills: [ "Sales", "Marketing" ], department: { "name":"Marketing" } }, { _id:4, firstName: "Steve", lastName: "J", email: "steve.j@abc.com", salary: 7000 }, { _id:5, firstName: "Kapil", lastName: "D", email: "kapil.d@abc.com", salary: 4500, skills: [ "Accounting", "Tax" ], department: { "name":"Finance" } }, { _id:6, firstName: "Amitabh", lastName: "B", email: "amitabh.b@abc.com", salary: 7000 } ])
The following example sorts the employees
collection in the ascending order of the firstName
field.
Example: sort() Copy
db.employees.find().sort({ firstName:1 })
Output
[ { _id: 6, firstName: 'Amitabh', lastName: 'B', email: 'amitabh.b@abc.com', salary: 7000 }, { _id: 3, firstName: 'James', lastName: 'Bond', email: 'jamesb@abc.com', salary: 7500, skills: [ 'Sales', 'Marketing' ], department: { name: 'Marketing' } }, { _id: 1, firstName: 'John', lastName: 'King', email: 'john.king@abc.com', salary: 5000, skills: [ 'Angular', 'React', 'MongoDB' ], department: { name: 'IT' } }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: 'kapil.d@abc.com', salary: 4500, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: 'sachin.t@abc.com', salary: 8000, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } }, { _id: 4, firstName: 'Steve', lastName: 'J', email: 'steve.j@abc.com', salary: 7000 } ]
You can use the cursor object to sort the result. The following returns the same result as above.
Example: cursor.sort()
var cursor = db.employees.find() cursor.sort({ firstName:1 })
The following example list the sorting on different fields.
Example: sort()
db.employees.find().sort({ _id: -1 })// sorts by descending order of _id db.employees.find().sort({ salary: -1 })// sorts by descending order of salary db.employees.find({salary: {$gt:5000}}).sort({ salary: -1 })// find where salary > 5000 and sorts the result by descending order of salary db.employees.find().sort({ firstName:1, salary: -1 })// sorts by ascending order of firstName and descending order of salary db.employees.find().sort({"department.name":1}) // sorts by embedded doc department.name
MongoDB uses the following comparison order, from lowest to highest for comparing values of different BSON types.
- MinKey (internal type)
- Null
- Numbers (ints, longs, doubles, decimals)
- Symbol, String
- Object
- Array
- BinData
- ObjectId
- Boolean
- Date
- Timestamp
- Regular Expression
- MaxKey (internal type)
Average Rating