Using Projection and Sorting in Queries
Using Projection and Sorting in Queries
In MongoDB, when we retrieve documents using the find() method, we often don't need all the fields. This is where projection helps — it allows us to specify which fields to include or exclude in the query results.
We also use sorting to arrange the documents in ascending or descending order based on a specific field, such as name, age, or date.
What is Projection?
Projection means selecting only specific fields to be returned in the output of a query. By default, MongoDB returns all fields in a document.
Basic Projection Example
Let’s first insert a few sample documents:
    db.students.insertMany([
      { name: "Alice", age: 22, course: "Math", grade: "A" },
      { name: "Bob", age: 23, course: "Physics", grade: "B" },
      { name: "Charlie", age: 21, course: "Chemistry", grade: "A" }
    ])Now, let’s retrieve only the name and course fields from each document:
    db.students.find({}, { name: 1, course: 1, _id: 0 })    { "name": "Alice", "course": "Math" }
    { "name": "Bob", "course": "Physics" }
    { "name": "Charlie", "course": "Chemistry" }
    Explanation: The first argument {} matches all documents. The second argument { name: 1, course: 1 } tells MongoDB to include only the name and course fields. We explicitly exclude the _id field using _id: 0.
Intuition Check
Q: What happens if you try to include and exclude fields together?
A: You can't mix inclusion and exclusion in the same projection (except for the _id field). MongoDB will throw an error.
Excluding Fields with Projection
If you want to exclude fields instead of including them, just set them to 0 in the projection:
    db.students.find({}, { grade: 0, _id: 0 })    { "name": "Alice", "age": 22, "course": "Math" }
    { "name": "Bob", "age": 23, "course": "Physics" }
    { "name": "Charlie", "age": 21, "course": "Chemistry" }
    Explanation: All fields are returned except grade and _id.
What is Sorting?
Sorting allows you to order the query result by one or more fields.
- 1for ascending order
- -1for descending order
Sorting Example 1: Ascending Order by Name
    db.students.find({}, { _id: 0 }).sort({ name: 1 })    { "name": "Alice", "age": 22, "course": "Math", "grade": "A" }
    { "name": "Bob", "age": 23, "course": "Physics", "grade": "B" }
    { "name": "Charlie", "age": 21, "course": "Chemistry", "grade": "A" }
    Explanation: The documents are sorted alphabetically by the name field.
Sorting Example 2: Descending Order by Age
    db.students.find({}, { _id: 0 }).sort({ age: -1 })    { "name": "Bob", "age": 23, "course": "Physics", "grade": "B" }
    { "name": "Alice", "age": 22, "course": "Math", "grade": "A" }
    { "name": "Charlie", "age": 21, "course": "Chemistry", "grade": "A" }
    Explanation: The documents are sorted by age in descending order.
Sorting Example 3: Sorting by Multiple Fields
Let’s sort first by grade (ascending), then by age (descending):
    db.students.find({}, { _id: 0 }).sort({ grade: 1, age: -1 })    { "name": "Alice", "age": 22, "course": "Math", "grade": "A" }
    { "name": "Charlie", "age": 21, "course": "Chemistry", "grade": "A" }
    { "name": "Bob", "age": 23, "course": "Physics", "grade": "B" }
    Explanation: Documents are sorted first by grade alphabetically (A before B), and within the same grade, they are sorted by age in descending order.
Recap
- Use projectionto include or exclude specific fields in the result
- Use sort()to order the result by one or more fields
- Use 1for ascending and-1for descending order
- Projection and sorting can be combined in a single query
Next Step
Now that you understand projection and sorting, you’re ready to dive deeper into advanced querying with MongoDB operators like $gt, $in, and $regex.












Comments
Loading comments...