Skip to content

#9 Sort and Limit Data in MongoDB

The Sort() and Limit() functions stand as essential tools in data analysis, often regarded as indispensable companions to MongoDB’s find().

Sort

We can sort data in an ascending order or a descending order.

  • To sort in ascending order, you can use 1
  • To sort in descending order, you can use -1

Example 1 : Sort data by the specific field “age” in a ascending order


> db.personnel.find({},{name: 1, age: 1}).sort({ "age": 1 })

[
  {
    _id: ObjectId('66053f848ccb6407b4ba21c9'),
    name: 'Sarah Lee',
    age: 27
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c5'),
    name: 'Jane Smith',
    age: 28
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cc'),
    name: 'Daniel Thompson',
    age: 29
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c4'),
    name: 'John Doe',
    age: 30
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cb'),
    name: 'Michelle Martinez',
    age: 31
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c7'),
    name: 'Emily Brown',
    age: 32
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cd'),
    name: 'Amanda Hall',
    age: 33
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21ca'),
    name: 'Alex Garcia',
    age: 34
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c6'),
    name: 'Michael Johnson',
    age: 35
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c8'),
    name: 'David Wilson',
    age: 40
  }
]

Example 2 : Sort data by the specific field “salary” in a descending order


> db.personnel.find({},{name: 1, salary: 1}).sort({ "salary": -1 })

[
  {
    _id: ObjectId('66053f848ccb6407b4ba21c8'),
    name: 'David Wilson',
    salary: 75000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c6'),
    name: 'Michael Johnson',
    salary: 70000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cd'),
    name: 'Amanda Hall',
    salary: 69000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21ca'),
    name: 'Alex Garcia',
    salary: 67000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cb'),
    name: 'Michelle Martinez',
    salary: 63000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c7'),
    name: 'Emily Brown',
    salary: 62000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c4'),
    name: 'John Doe',
    salary: 60000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cc'),
    name: 'Daniel Thompson',
    salary: 58000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c5'),
    name: 'Jane Smith',
    salary: 55000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c9'),
    name: 'Sarah Lee',
    salary: 50000
  }
]

Example 3: Sort data by two fields in different orders.

  • let’s say you want to sort the employees first by their age (in ascending order) and then by their salary (in descending order).
  • This will return the employees sorted first by age and then by salary.

> db.personnel.find({},{name: 1, age: 1, salary: 1}).sort({ "age": 1, "salary": -1 })

[
  {
    _id: ObjectId('66053f848ccb6407b4ba21c9'),
    name: 'Sarah Lee',
    age: 27,
    salary: 50000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c5'),
    name: 'Jane Smith',
    age: 28,
    salary: 55000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cc'),
    name: 'Daniel Thompson',
    age: 29,
    salary: 58000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c4'),
    name: 'John Doe',
    age: 30,
    salary: 60000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cb'),
    name: 'Michelle Martinez',
    age: 31,
    salary: 63000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c7'),
    name: 'Emily Brown',
    age: 32,
    salary: 62000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cd'),
    name: 'Amanda Hall',
    age: 33,
    salary: 69000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21ca'),
    name: 'Alex Garcia',
    age: 34,
    salary: 67000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c6'),
    name: 'Michael Johnson',
    age: 35,
    salary: 70000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c8'),
    name: 'David Wilson',
    age: 40,
    salary: 75000
  }
]

Limit

The limit() function is used to specify the maximum number of documents that should be returned in a query result.

Example 4: retrieve only 3 documents

> db.personnel.find().limit(3)

[
  {
    _id: ObjectId('66053f848ccb6407b4ba21c4'),
    name: 'John Doe',
    age: 30,
    working_experience: 5,
    department: 'Engineering',
    salary: 60000,
    skills: [ 'JavaScript', 'Node.js', 'MongoDB', 'React' ]
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c5'),
    name: 'Jane Smith',
    age: 28,
    working_experience: 4,
    department: 'Marketing',
    salary: 55000,
    skills: [ 'Digital Marketing', 'SEO', 'Social Media Management' ]
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c6'),
    name: 'Michael Johnson',
    age: 35,
    working_experience: 8,
    department: 'Finance',
    salary: 70000,
    skills: [ 'Accounting', 'Financial Analysis', 'Excel' ]
  }
]

Example 5: Combining Sort and Limit

> db.personnel.find({},{name: 1, age: 1, salary: 1}).sort({"salary": -1 }).limit(3)

[
  {
    _id: ObjectId('66053f848ccb6407b4ba21c8'),
    name: 'David Wilson',
    age: 40,
    salary: 75000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21c6'),
    name: 'Michael Johnson',
    age: 35,
    salary: 70000
  },
  {
    _id: ObjectId('66053f848ccb6407b4ba21cd'),
    name: 'Amanda Hall',
    age: 33,
    salary: 69000
  }
]