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:

  1. 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: "[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.

Example: sort()
db.employees.find().sort({ firstName:1 })
Output
[
  {
    _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.

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.

  1. MinKey (internal type)
  2. Null
  3. Numbers (ints, longs, doubles, decimals)
  4. Symbol, String
  5. Object
  6. Array
  7. BinData
  8. ObjectId
  9. Boolean
  10. Date
  11. Timestamp
  12. Regular Expression
  13. MaxKey (internal type)

Visit BSON type comparison order for more information.