MongoDB Projection Practice Questions with Solutions

Introduction

Projection in MongoDB is used to control which fields are returned when you query documents. Instead of displaying an entire document, you can return only the information you need, such as a student’s name and course. Projection is especially useful when documents contain many fields. In this chapter, you will practice including and excluding fields, hiding _id, working with nested fields, and combining projection with filtering and sorting. MongoDB Projection Practice questions with solutions to help you understand the concepts.

Q1. Display Only Student Names

Problem Statement:
Find all students but display only their name field.

MongoDB Query:

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

Expected Output:

{ _id: ObjectId("..."), name: "Rahul" }
{ _id: ObjectId("..."), name: "Priya" }
{ _id: ObjectId("..."), name: "Aman" }

Explanation:
name: 1 tells MongoDB to include the name field in the result.

By default, _id is also displayed.


Q2. Display Student Name and Age

Problem Statement:
Display only the name and age of every student.

MongoDB Query:

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

Expected Output:

{
  _id: ObjectId("..."),
  name: "Rahul",
  age: 16
}
{
  _id: ObjectId("..."),
  name: "Priya",
  age: 17
}

Explanation:
Both name and age are included using 1.


Q3. Hide the _id Field

Problem Statement:
Display student names and ages without displaying the automatically generated _id.

MongoDB Query:

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

Expected Output:

{
  name: "Rahul",
  age: 16
}
{
  name: "Priya",
  age: 17
}

Explanation:
_id: 0 explicitly excludes the _id field.

The _id field is included by default, so you must specifically exclude it when you don’t want it in the output.


Q4. Display Only Student Names and Courses

Problem Statement:
Find all students and display only their name and course.

MongoDB Query:

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

Expected Output:

{
  name: "Rahul",
  course: "Python"
}
{
  name: "Priya",
  course: "Java"
}

Explanation:
The projection includes name and course while excluding _id.


Q5. Find Python Students and Display Only Their Names

Problem Statement:
Find students enrolled in the Python course, but display only their names.

MongoDB Query:

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

Expected Output:

{ name: "Rahul" }
{ name: "Aman" }

Explanation:
The first object is the filter:

{ course: "Python" }

The second object is the projection:

{
  _id: 0,
  name: 1
}

So MongoDB finds Python students and returns only their names.


Q6. Display a Nested Field

Problem Statement:
Each student has an address document. Display the student’s name and only the city from the nested address document.

Example document:

{
  name: "Rahul",
  age: 16,
  address: {
    city: "Delhi",
    pincode: 110075
  }
}

MongoDB Query:

db.students.find(
  {},
  {
    _id: 0,
    name: 1,
    "address.city": 1
  }
)

Expected Output:

{
  name: "Rahul",
  address: {
    city: "Delhi"
  }
}

Explanation:
Dot notation is used to project a nested field:

"address.city": 1

Q7. Exclude the Age Field

Problem Statement:
Display student documents but hide the age field.

MongoDB Query:

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

Expected Output:

{
  _id: ObjectId("..."),
  name: "Rahul",
  course: "Python"
}

Explanation:
age: 0 tells MongoDB not to return the age field.

When excluding fields, MongoDB returns the other fields unless they are also excluded.


Q8. Display Student Name, Course, and Skills

Problem Statement:
Display only the student’s name, course, and skills.

MongoDB Query:

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

Expected Output:

{
  name: "Rahul",
  course: "Python",
  skills: ["Python", "SQL", "HTML"]
}

Explanation:
Projection can include an array field just like a normal field.


Q9. Find Students Above Age 15 and Display Name and Age

Problem Statement:
Find students older than 15 and display only their names and ages.

MongoDB Query:

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

Expected Output:

{
  name: "Rahul",
  age: 16
}
{
  name: "Priya",
  age: 17
}

Explanation:
Projection works together with query operators.

Here:

{ age: { $gt: 15 } }

filters the documents, while:

{
  _id: 0,
  name: 1,
  age: 1
}

controls which fields are returned.


Q10. Display Name and City, Sort by Name

Problem Statement:
Find all students, display only their name and city, and sort the results alphabetically by name.

MongoDB Query:

db.students.find(
  {},
  {
    _id: 0,
    name: 1,
    "address.city": 1
  }
).sort({
  name: 1
})

Expected Output:

{
  name: "Aman",
  address: {
    city: "Delhi"
  }
}
{
  name: "Neha",
  address: {
    city: "Mumbai"
  }
}
{
  name: "Rahul",
  address: {
    city: "Delhi"
  }
}

Explanation:
This query performs three operations:

  1. Finds all documents.
  2. Projects only name and address.city.
  3. Sorts the results by name in ascending order.

Key Takeaways

  • Projection controls which fields MongoDB returns.
  • field: 1 means include the field.
  • field: 0 means exclude the field.
  • _id is included by default.
  • Use _id: 0 when you want to hide _id.
  • Projection can be used with nested fields using dot notation.
  • Projection works together with filters such as $gt, $in, and $or.
  • Projection does not change the documents stored in MongoDB.
  • You can combine projection with operations such as sort() and limit().

FAQs

1. What is projection in MongoDB?

Projection is used to specify which fields MongoDB should return from matching documents.

For example:

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

2. What does 1 mean in MongoDB projection?

1 means include this field in the returned documents.

{
  name: 1,
  age: 1
}

3. What does 0 mean in MongoDB projection?

0 means exclude this field from the returned documents.

{
  age: 0
}

4. How do you hide _id in MongoDB projection?

Use:

{
  _id: 0,
  name: 1
}

MongoDB includes _id by default, so _id: 0 is commonly used when it should not appear in the result.

5. Can projection be used with nested documents?

Yes. Use dot notation.

db.students.find(
  {},
  {
    _id: 0,
    name: 1,
    "address.city": 1
  }
)

6. Can projection be combined with a query filter?

Yes. The find() method accepts a filter and a projection.

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

7. Can you mix inclusion and exclusion in MongoDB projection?

Generally, you should not mix inclusion and exclusion in the same projection. The main exception is the _id field.

For example, this is valid:

{
  _id: 0,
  name: 1,
  age: 1
}

But mixing unrelated inclusion and exclusion fields is not valid:

{
  name: 1,
  age: 0
}

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top