Paramclasses Academy - Blog

Tutorials Details

←
Post 15 of 35
→

MongoDB Projection, Sorting, Limit and Skip with Examples

MongoDB Projection, Sorting, Limit and Skip with Examples

November 5, 2026

MongoDB Projection, Sorting, Limit and Skip with Examples

Welcome to Param Classes!

When working with MongoDB, retrieving documents is not always enough. Sometimes, we only need specific fields, want records in a particular order, or need to display a limited number of results.

MongoDB provides four useful features to manage query results:

  • Projection – Select specific fields from documents.

  • Sorting – Arrange documents in ascending or descending order.

  • Limit – Restrict the number of documents returned.

  • Skip – Skip a specified number of documents.

In this tutorial, you will learn how to use all four features with practical MongoDB Shell (mongosh) examples.

✦ 1. What Is Projection in MongoDB?

Projection in MongoDB is used to select specific fields from documents instead of displaying every field.

For example, imagine a student collection containing name, age, course, email and city.

If we only want to display student names and courses, we can use projection.

Syntax:

db.collection.find(
    { condition },
    { fieldName: 1 }
)

Here:

  • 1 means include the field.

  • 0 means exclude the field.

  • _id is included by default unless explicitly excluded.

Example:

db.students.find(
    {},
    {
        name: 1,
        course: 1
    }
)

This displays only the name, course and default _id fields.

Example without _id:

db.students.find(
    {},
    {
        name: 1,
        course: 1,
        _id: 0
    }
)

Now MongoDB displays only the name and course fields.

✦ 2. Include and Exclude Fields in Projection

MongoDB projection supports two main approaches.

A. Include Specific Fields

Use 1 to include the required fields.

db.students.find(
    {},
    {
        name: 1,
        age: 1,
        _id: 0
    }
)

This displays only name and age.

B. Exclude Specific Fields

Use 0 to exclude unwanted fields.

db.students.find(
    {},
    {
        email: 0,
        city: 0
    }
)

This displays all fields except email and city.

Important Rule:

You cannot normally mix inclusion (1) and exclusion (0) in the same projection, except for the _id field.

✦ 3. What Is Sorting in MongoDB?

Sorting is used to arrange documents according to a particular field.

MongoDB provides the sort() method to organize query results.

There are two types of sorting:

  1. Ascending Order

  2. Descending Order

Syntax:

db.collection.find().sort({
    fieldName: 1
})

Here:

  • 1 means ascending order.

  • -1 means descending order.

A. Ascending Sorting

Ascending sorting arranges values from smallest to largest or alphabetically from A to Z.

Example:

db.students.find().sort({
    age: 1
})

This displays students according to age in ascending order.

B. Descending Sorting

Descending sorting arranges values from largest to smallest or alphabetically from Z to A.

Example:

db.students.find().sort({
    age: -1
})

This displays students according to age in descending order.

C. Sorting Using Multiple Fields

MongoDB also allows sorting using multiple fields.

Example:

db.students.find().sort({
    course: 1,
    age: -1
})

Here:

  • First, documents are sorted by course in ascending order.

  • If multiple documents have the same course, they are sorted by age in descending order.

✦ 4. What Is limit() in MongoDB?

The limit() method is used to restrict the number of documents returned by a query.

For example, if a collection contains 100 student records but you only want to display the first 5 records, you can use limit().

Syntax:

db.collection.find().limit(number)

Example:

db.students.find().limit(5)

This returns a maximum of 5 documents.

Limit with Sorting

You can combine sorting and limit to retrieve records in a particular order.

Example:

db.students.find().sort({
    age: -1
}).limit(3)

This displays the 3 oldest students based on age.

✦ 5. What Is skip() in MongoDB?

The skip() method is used to skip a specified number of documents before returning the remaining results.

It is especially useful when implementing pagination.

Syntax:

db.collection.find().skip(number)

Example:

db.students.find().skip(3)

This skips the first 3 documents and returns the remaining documents.

Skip with Sorting

For better and consistent results, use sorting before skipping documents.

Example:

db.students.find().sort({
    age: 1
}).skip(2)

This sorts students by age in ascending order and skips the first 2 documents.

✦ 6. Combining sort(), limit() and skip()

MongoDB allows you to combine these methods in a single query.

Example:

db.students.find()
    .sort({ age: 1 })
    .skip(2)
    .limit(3)

This query performs three operations:

  1. Sorts students by age in ascending order.

  2. Skips the first 2 documents.

  3. Returns the next 3 documents.

Result: Maximum 3 documents are returned after skipping the first 2 sorted records.

MongoDB applies sorting before skipping and limiting when processing the cursor, regardless of the order in which these cursor methods are chained.

✦ 7. Practical Example: Create a Student Collection

Now let's understand all four features using a practical example.

Open MongoDB Shell (mongosh) and select a database:

use college

Insert sample student records:

db.students.insertMany([
    {
        name: "Rahul",
        age: 22,
        course: "MongoDB",
        city: "Raipur",
        marks: 85
    },
    {
        name: "Amit",
        age: 24,
        course: "Java",
        city: "Delhi",
        marks: 90
    },
    {
        name: "Priya",
        age: 21,
        course: "Python",
        city: "Mumbai",
        marks: 95
    },
    {
        name: "Neha",
        age: 26,
        course: "MongoDB",
        city: "Raipur",
        marks: 88
    },
    {
        name: "Rohit",
        age: 23,
        course: "PHP",
        city: "Pune",
        marks: 78
    },
    {
        name: "Anjali",
        age: 25,
        course: "Java",
        city: "Jaipur",
        marks: 92
    }
])

Display all student records:

db.students.find()

Now let's perform different operations.

Example 1: Display Only Names and Courses

db.students.find(
    {},
    {
        name: 1,
        course: 1,
        _id: 0
    }
)

Example 2: Sort Students by Marks

db.students.find().sort({
    marks: -1
})

This displays students from highest marks to lowest marks.

Example 3: Display Only Top 3 Students

db.students.find()
    .sort({ marks: -1 })
    .limit(3)

This returns the 3 students with the highest marks.

Example 4: Skip the First 2 Students

db.students.find().skip(2)

This skips the first 2 documents.

Example 5: Display the Next 2 Students After Skipping 2

db.students.find()
    .sort({ age: 1 })
    .skip(2)
    .limit(2)

This sorts students by age, skips the first 2 and returns the next 2 documents.

✦ 8. How to Implement Pagination in MongoDB?

Pagination means dividing a large collection of records into smaller pages.

For example, if a website contains 100 student records, displaying all records on one page may not be convenient.

Instead, we can display 10 records per page.

MongoDB uses skip() and limit() to implement pagination.

Formula:

skip = (pageNumber - 1) * pageSize

Here:

  • pageNumber represents the current page.

  • pageSize represents the number of records per page.

Example: Page 1

db.students.find()
    .sort({ _id: 1 })
    .skip(0)
    .limit(10)

This displays the first 10 records.

Example: Page 2

db.students.find()
    .sort({ _id: 1 })
    .skip(10)
    .limit(10)

This skips the first 10 records and displays the next 10.

Example: Page 3

db.students.find()
    .sort({ _id: 1 })
    .skip(20)
    .limit(10)

This skips the first 20 records and displays the next 10.

Important: Use a consistent sort order for pagination. Sorting by a unique field such as _id helps avoid inconsistent ordering when multiple documents have the same sort value. For very large datasets, range-based pagination can be more efficient than skipping many documents.

✦ 9. Difference Between Projection, Sorting, Limit and Skip

Feature Purpose
Projection Select specific fields
sort() Arrange documents
limit() Restrict result count
skip() Skip documents

Example:

db.students.find(
    {},
    {
        name: 1,
        marks: 1,
        _id: 0
    }
)
.sort({ marks: -1 })
.skip(1)
.limit(3)

This query:

  • Displays only name and marks.

  • Sorts students by marks in descending order.

  • Skips the first student.

  • Returns the next 3 students.

✦ 10. Important Points to Remember

Before using these MongoDB methods, remember the following:

  1. Projection helps retrieve only the required fields.

  2. Use 1 for inclusion and 0 for exclusion.

  3. The _id field is included by default.

  4. Use sort({ field: 1 }) for ascending order.

  5. Use sort({ field: -1 }) for descending order.

  6. Use limit() to control the number of returned documents.

  7. Use skip() to skip documents.

  8. Combine sort(), skip() and limit() for pagination.

  9. Use a unique sort field for consistent pagination.

  10. Avoid unnecessarily large skip values in large collections.

✦ 11. Practical Exercise

Now it's time to practice these MongoDB commands yourself.

Create an employees collection:

db.employees.insertMany([
    { name: "Aman", age: 25, department: "IT", salary: 30000 },
    { name: "Riya", age: 28, department: "HR", salary: 35000 },
    { name: "Karan", age: 24, department: "IT", salary: 28000 },
    { name: "Pooja", age: 30, department: "Sales", salary: 40000 },
    { name: "Vikas", age: 27, department: "HR", salary: 32000 },
    { name: "Neha", age: 26, department: "IT", salary: 38000 }
])

Practice Tasks:

  1. Display only employee names and departments.

  2. Display all employees in ascending order of age.

  3. Display employees in descending order of salary.

  4. Display only the top 3 highest-paid employees.

  5. Skip the first 2 employee records.

  6. Display the next 2 employees after skipping 2 records.

  7. Display employees sorted by age and show only their names and salaries.

  8. Implement pagination with 2 employees per page.

Try completing these tasks using find(), projection, sort(), limit() and skip().

✦ 12. Conclusion

In this tutorial, we learned how to manage MongoDB query results using Projection, Sorting, Limit and Skip.

We covered practical examples, field selection, ascending and descending sorting, result limitations and pagination.

Remember:

  • Projection selects the required fields.

  • Sorting arranges documents in a particular order.

  • Limit restricts the number of results.

  • Skip ignores a specified number of documents.

  • Combining these methods makes database queries more organized and useful.

These concepts are important for building real-world applications, dashboards and data-driven websites.

Keep practicing with Param Classes and continue improving your MongoDB database skills step by step.

←
Post 15 of 35
→