Sorting Documents in MongoDB: The sort() Method
Learn how to effectively order documents in MongoDB collections using the sort() method. This guide will cover the various sorting criteria and techniques, allowing you to sort documents based on specific fields in ascending or descending order. Enhance your data analysis capabilities by mastering the sort() method.
MongoDB: Sort Documents in Collection using sort()
In MongoDB, you can sort documents using the sort()
method. This method is used to order the documents in a collection based on specified criteria.
The sort()
method can be used directly on the db.collection.find()
cursor or on a cursor object obtained from find()
.
Sample Data
Insert the following documents into the employees
collection:
Insert 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 }
])
Using sort()
The sort()
method sorts the documents based on specified fields. The sorting order is defined as follows:
1
for ascending order.-1
for descending order.
Syntax
db.collection.find().sort({ field1: 1, field2: -1, ... })
Examples
1. Sort documents in ascending order of the firstName
field:
Example: Sort by First Name
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 } ]
2. Sort by _id
in descending order:
Example: Sort by _id Descending
db.employees.find().sort({ _id: -1 })
Output:
[ { _id: 6, firstName: 'Amitabh', lastName: 'B', email: 'amitabh.b@abc.com', salary: 7000 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: 'kapil.d@abc.com', salary: 4500, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } }, { _id: 4, firstName: 'Steve', lastName: 'J', email: 'steve.j@abc.com', salary: 7000 }, { _id: 3, firstName: 'James', lastName: 'Bond', email: 'jamesb@abc.com', salary: 7500, skills: [ 'Sales', 'Marketing' ], department: { name: 'Marketing' } }, { _id: 2, firstName: 'Sachin', lastName: 'T', email: 'sachin.t@abc.com', salary: 8000, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } }, { _id: 1, firstName: 'John', lastName: 'King', email: 'john.king@abc.com', salary: 5000, skills: [ 'Angular', 'React', 'MongoDB' ], department: { name: 'IT' } } ]
3. Sort by salary
in descending order:
Example: Sort by Salary Descending
db.employees.find().sort({ salary: -1 })
Output:
[ { _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: 6, firstName: 'Amitabh', lastName: 'B', email: 'amitabh.b@abc.com', salary: 7000 }, { _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' } } ]
4. Sort by multiple fields:
Example: Sort by Multiple Fields
db.employees.find().sort({ firstName: 1, salary: -1 })
Output:
[ { _id: 6, firstName: 'Amitabh', lastName: 'B', email: 'amitabh.b@abc.com', salary: 7000 }, { _id: 1, firstName: 'John', lastName: 'King', email: 'john.king@abc.com', salary: 5000, skills: [ 'Angular', 'React', 'MongoDB' ], department: { name: 'IT' } }, { _id: 3, firstName: 'James', lastName: 'Bond', email: 'jamesb@abc.com', salary: 7500, skills: [ 'Sales', 'Marketing' ], department: { name: 'Marketing' } }, { _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 }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: 'kapil.d@abc.com', salary: 4500, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } } ]
5. Sort by a field in an embedded document:
Example: Sort by Embedded Document Field
db.employees.find().sort({ "department.name": 1 })
Output:
[ { _id: 2, firstName: 'Sachin', lastName: 'T', email: 'sachin.t@abc.com', salary: 8000, skills: [ 'Accounting', 'Tax' ], department: { name: 'Finance' } }, { _id: 5, firstName: 'Kapil', lastName: 'D', email: 'kapil.d@abc.com', salary: 4500, 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: 1, firstName: 'John', lastName: 'King', email: 'john.king@abc.com', salary: 5000, skills: [ 'Angular', 'React', 'MongoDB' ], department: { name: 'IT' } }, { _id: 4, firstName: 'Steve', lastName: 'J', email: 'steve.j@abc.com', salary: 7000 }, { _id: 6, firstName: 'Amitabh', lastName: 'B', email: 'amitabh.b@abc.com', salary: 7000 } ]
Note: MongoDB uses a specific comparison order for BSON types. For detailed comparison order, refer to the BSON type documentation.