MongoDB Find Documents Practice Questions with Solutions

Introduction

The find() method is one of the most important MongoDB operations because it is used to retrieve documents from a collection. MongoDB allows you to find all documents, search for specific values, combine multiple conditions, work with arrays and nested fields, and control which fields appear in the result. In this chapter, you will practice practical MongoDB document-search queries using find() and related methods. MongoDB Find Documents practice questions with solutions to help you understand the concepts.

Q1. Find all documents

Problem Statement

Display all documents stored in the students collection.

MongoDB Command / Query

db.students.find()

Expected Output

[
  {
    _id: ObjectId('...'),
    name: 'Rahul',
    age: 15,
    course: 'Python'
  },
  {
    _id: ObjectId('...'),
    name: 'Priya',
    age: 16,
    course: 'JavaScript'
  },
  {
    _id: ObjectId('...'),
    name: 'Aman',
    age: 17,
    course: 'MongoDB'
  }
]

Explanation

find() without a filter returns all documents from the collection.

db.students.find()

Here, students is the collection and find() retrieves its documents.


Q2. Find one document using findOne()

Problem Statement

Find the first student whose course is "Python".

MongoDB Command / Query

db.students.findOne({
    course: "Python"
})

Expected Output

{
  _id: ObjectId('...'),
  name: 'Rahul',
  age: 15,
  course: 'Python'
}

Explanation

findOne() returns a single matching document.

If multiple students have "Python" as their course, findOne() returns only one matching document.


Q3. Find documents by an exact field value

Problem Statement

Find all students whose age is 16.

MongoDB Command / Query

db.students.find({
    age: 16
})

Expected Output

[
  {
    _id: ObjectId('...'),
    name: 'Priya',
    age: 16,
    course: 'JavaScript'
  }
]

Explanation

The filter:

{
    age: 16
}

tells MongoDB to return documents where the age field has the value 16.


Q4. Find documents using a comparison operator

Problem Statement

Find all students whose age is greater than 15.

MongoDB Command / Query

db.students.find({
    age: {
        $gt: 15
    }
})

Expected Output

[
  {
    _id: ObjectId('...'),
    name: 'Priya',
    age: 16,
    course: 'JavaScript'
  },
  {
    _id: ObjectId('...'),
    name: 'Aman',
    age: 17,
    course: 'MongoDB'
  }
]

Explanation

$gt means greater than.

$gt: 15

means MongoDB should find documents where age is greater than 15.

Other commonly used comparison operators include:

$gt  → greater than
$gte → greater than or equal to
$lt  → less than
$lte → less than or equal to
$eq  → equal to
$ne  → not equal to

Q5. Find documents using two conditions

Problem Statement

Find students whose age is 16 and whose course is "JavaScript".

MongoDB Command / Query

db.students.find({
    age: 16,
    course: "JavaScript"
})

Expected Output

[
  {
    _id: ObjectId('...'),
    name: 'Priya',
    age: 16,
    course: 'JavaScript'
  }
]

Explanation

When multiple field conditions are placed in the same query object, MongoDB treats them as an AND condition.

The document must satisfy both:

age = 16
AND
course = JavaScript

Q6. Find documents using the $or operator

Problem Statement

Find students whose course is either "Python" or "MongoDB".

MongoDB Command / Query

db.students.find({
    $or: [
        { course: "Python" },
        { course: "MongoDB" }
    ]
})

Expected Output

[
  {
    _id: ObjectId('...'),
    name: 'Rahul',
    age: 15,
    course: 'Python'
  },
  {
    _id: ObjectId('...'),
    name: 'Aman',
    age: 17,
    course: 'MongoDB'
  }
]

Explanation

$or returns documents that satisfy at least one of the specified conditions.

So the query means:

course = Python
OR
course = MongoDB

Q7. Find documents containing a specific array value

Problem Statement

Find all students who have "MongoDB" in their skills array.

MongoDB Command / Query

db.students.find({
    skills: "MongoDB"
})

Expected Output

[
  {
    _id: ObjectId('...'),
    name: 'Neha',
    age: 16,
    skills: [
      'Python',
      'MongoDB',
      'JavaScript'
    ]
  }
]

Explanation

MongoDB can search for a value directly inside an array field.

You do not need to manually loop through the array.


Q8. Find documents using a nested field

Problem Statement

Find all students whose city is "Delhi" inside their nested address document.

MongoDB Command / Query

db.students.find({
    "address.city": "Delhi"
})

Expected Output

[
  {
    _id: ObjectId('...'),
    name: 'Rohit',
    age: 15,
    address: {
      city: 'Delhi',
      area: 'Dwarka'
    }
  }
]

Explanation

MongoDB uses dot notation to search fields inside nested documents.

For this document:

address: {
    city: "Delhi",
    area: "Dwarka"
}

you can search the city using:

"address.city": "Delhi"

Q9. Find documents and return only selected fields

Problem Statement

Find all students but display only their name and course, hiding the _id field.

MongoDB Command / Query

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

Expected Output

[
  {
    name: 'Rahul',
    course: 'Python'
  },
  {
    name: 'Priya',
    course: 'JavaScript'
  },
  {
    name: 'Aman',
    course: 'MongoDB'
  }
]

Explanation

The second object is called the projection.

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

means:

  • name: 1 → include name
  • course: 1 → include course
  • _id: 0 → exclude _id

By default, _id is included unless you explicitly exclude it.


Q10. Find documents and limit the number of results

Problem Statement

Find students whose age is greater than 14, but display only the first two matching documents.

MongoDB Command / Query

db.students.find({
    age: {
        $gt: 14
    }
}).limit(2)

Expected Output

[
  {
    _id: ObjectId('...'),
    name: 'Rahul',
    age: 15,
    course: 'Python'
  },
  {
    _id: ObjectId('...'),
    name: 'Priya',
    age: 16,
    course: 'JavaScript'
  }
]

Explanation

limit(2) restricts the number of documents returned to two.

This is useful when a collection contains many documents but you only want a small number of results.

Key Takeaways

  • find() retrieves documents from a MongoDB collection.
  • find() without a filter returns all matching documents.
  • findOne() returns a single matching document.
  • Exact values can be used to filter documents.
  • Comparison operators such as $gt, $lt, $gte, and $lte help create range queries.
  • Multiple conditions in one query object work as an AND condition.
  • $or allows you to match one of several conditions.
  • MongoDB can search values inside arrays.
  • Dot notation is used to search nested document fields.
  • Projection controls which fields are returned.
  • limit() restricts the number of documents returned.

FAQs

1. What is the find() method in MongoDB?

find() is a MongoDB method used to retrieve documents from a collection.

db.students.find()

It can be used with filters to retrieve specific documents.

2. What is the difference between find() and findOne()?

find() can return multiple matching documents, while findOne() returns only one matching document.

db.students.find({ age: 16 })

can return multiple documents, whereas:

db.students.findOne({ age: 16 })

returns one matching document.

3. How do I find a document with a specific value?

Pass the field and value inside the find() filter:

db.students.find({
    course: "Python"
})

4. How do I find documents where a number is greater than a value?

Use the $gt comparison operator:

db.students.find({
    age: {
        $gt: 15
    }
})

5. How do I search a nested field in MongoDB?

Use dot notation:

db.students.find({
    "address.city": "Delhi"
})

This searches the city field inside the address document.

6. How do I search for a value inside a MongoDB array?

You can specify the value directly:

db.students.find({
    skills: "Python"
})

MongoDB will match documents where the skills array contains "Python".

7. How can I return only specific fields from MongoDB?

Use projection as the second argument to find():

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

This returns only name and course while excluding _id.

SEO Package

SEO Title

MongoDB Find Documents Practice Questions with Solutions

Meta Description

MongoDB Find Documents Practice Questions with Solutions to learn find(), findOne(), filters, comparison operators, nested fields, arrays, projection, and limit.

SEO Slug

mongodb-find-documents-practice-questions-with-solutions

Focus Keywords

  1. MongoDB Find Documents Practice Questions
  2. MongoDB Find Documents Practice Questions with Solutions
  3. MongoDB find() Practice Questions
  4. MongoDB findOne() Practice Questions
  5. MongoDB Find Documents
  6. MongoDB find Query
  7. MongoDB Query Practice Questions
  8. MongoDB Search Documents

Relevant Tags

MongoDB, MongoDB Find Documents, MongoDB find, MongoDB findOne, MongoDB Queries, MongoDB Query Practice, MongoDB Practice Questions, MongoDB Practice Questions with Solutions, MongoDB Tutorial, MongoDB for Beginners, MongoDB Query Operators, MongoDB Comparison Operators, MongoDB Filters, MongoDB Projection, MongoDB Limit, MongoDB Arrays, MongoDB Nested Documents, MongoDB Dot Notation, MongoDB mongosh, MongoDB CRUD Operations, NoSQL Database, Database Practice Questions, MongoDB Exercises, MongoDB Learning, MongoDB Development

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

Scroll to Top