Sort Documents in MongoDB Collection
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.
db.employees.insertMany([
{
_id:1,
firstName: "John",
lastName: "King",
email: "[email protected]",
salary: 5000,
skills: [ "Angular", "React", "MongoDB" ],
department: {
"name":"IT"
}
},
{
_id:2,
firstName: "Sachin",
lastName: "T",
email: "[email protected]",
salary: 8000,
skills: [ "Accounting", "Tax" ],
department: {
"name":"Finance"
}
},
{
_id:3,
firstName: "James",
lastName: "Bond",
email: "[email protected]",
salary: 7500,
skills: [ "Sales", "Marketing" ],
department: {
"name":"Marketing"
}
},
{
_id:4,
firstName: "Steve",
lastName: "J",
email: "[email protected]",
salary: 7000
},
{
_id:5,
firstName: "Kapil",
lastName: "D",
email: "[email protected]",
salary: 4500,
skills: [ "Accounting", "Tax" ],
department: {
"name":"Finance"
}
},
{
_id:6,
firstName: "Amitabh",
lastName: "B",
email: "[email protected]",
salary: 7000
}
])
The following example sorts the employees
collection in the ascending order of the firstName
field.
db.employees.find().sort({ firstName:1 })
[
{
_id: 6,
firstName: 'Amitabh',
lastName: 'B',
email: '[email protected]',
salary: 7000
},
{
_id: 3,
firstName: 'James',
lastName: 'Bond',
email: '[email protected]',
salary: 7500,
skills: [ 'Sales', 'Marketing' ],
department: { name: 'Marketing' }
},
{
_id: 1,
firstName: 'John',
lastName: 'King',
email: '[email protected]',
salary: 5000,
skills: [ 'Angular', 'React', 'MongoDB' ],
department: { name: 'IT' }
},
{
_id: 5,
firstName: 'Kapil',
lastName: 'D',
email: '[email protected]',
salary: 4500,
skills: [ 'Accounting', 'Tax' ],
department: { name: 'Finance' }
},
{
_id: 2,
firstName: 'Sachin',
lastName: 'T',
email: '[email protected]',
salary: 8000,
skills: [ 'Accounting', 'Tax' ],
department: { name: 'Finance' }
},
{
_id: 4,
firstName: 'Steve',
lastName: 'J',
email: '[email protected]',
salary: 7000
}
]
You can use the cursor object to sort the result. The following returns the same result as above.
var cursor = db.employees.find()
cursor.sort({ firstName:1 })
The following example list the sorting on different fields.
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)
Visit BSON type comparison order for more information.