MongoDB Querying Arrays Practice Questions with Solutions

Introduction

Arrays are commonly used in MongoDB documents to store multiple values in a single field, such as skills, courses, subjects, or product categories. MongoDB provides several ways to query these array fields. In this chapter, you will practice finding documents using direct array values, multiple array elements, array positions, $all, $size, $elemMatch, and other practical array queries. MongoDB Querying Arrays practice questions with solutions to help you understand the cocepts.

Q1. Find Students Who Know Python

Problem Statement:
Suppose students have a skills array. Find all students whose skills include "Python".

MongoDB Query:

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

Expected Output:

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

Explanation:
When you query an array field with a single value, MongoDB finds documents where that value exists as an array element.


Q2. Find Students Who Know Both Python and SQL

Problem Statement:
Find students whose skills array contains both "Python" and "SQL".

MongoDB Query:

db.students.find({
  skills: {
    $all: ["Python", "SQL"]
  }
})

Expected Output:

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

Explanation:
$all matches documents where the array contains all specified values.


Q3. Find Students Having Exactly 3 Skills

Problem Statement:
Find students whose skills array contains exactly three elements.

MongoDB Query:

db.students.find({
  skills: {
    $size: 3
  }
})

Expected Output:

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

Explanation:
$size checks the exact number of elements in an array.


Q4. Find Students Having Exactly 2 Skills

Problem Statement:
Find students whose skills array contains exactly two skills.

MongoDB Query:

db.students.find({
  skills: {
    $size: 2
  }
})

Expected Output:

{
  name: "Priya",
  skills: ["Java", "MongoDB"]
}

Explanation:
$size: 2 matches only arrays containing exactly two elements.


Q5. Find Students with a Subject Having Marks Above 80

Problem Statement:
Suppose every student has a results array containing subject names and marks. Find students who have at least one subject where marks are greater than 80.

Example document:

{
  name: "Aman",
  results: [
    { subject: "Python", marks: 85 },
    { subject: "SQL", marks: 72 }
  ]
}

MongoDB Query:

db.students.find({
  results: {
    $elemMatch: {
      marks: { $gt: 80 }
    }
  }
})

Expected Output:

{
  name: "Aman",
  results: [
    { subject: "Python", marks: 85 },
    { subject: "SQL", marks: 72 }
  ]
}

Explanation:
$elemMatch checks whether at least one array element satisfies the specified condition.


Q6. Find Students Who Have Either Python or Java

Problem Statement:
Find students whose skills array contains either "Python" or "Java".

MongoDB Query:

db.students.find({
  skills: {
    $in: ["Python", "Java"]
  }
})

Expected Output:

{
  name: "Rahul",
  skills: ["Python", "SQL", "HTML"]
}
{
  name: "Priya",
  skills: ["Java", "MongoDB"]
}

Explanation:
$in matches an array field when at least one array element matches one of the specified values.


Q7. Find Students Who Do Not Have PHP as a Skill

Problem Statement:
Find students whose skills array does not contain "PHP".

MongoDB Query:

db.students.find({
  skills: {
    $nin: ["PHP"]
  }
})

Expected Output:

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

Explanation:
$nin matches documents where none of the array elements are in the specified list.


Q8. Find Students Having Both HTML and CSS

Problem Statement:
Find students whose skills array contains both "HTML" and "CSS".

MongoDB Query:

db.students.find({
  skills: {
    $all: ["HTML", "CSS"]
  }
})

Expected Output:

{
  name: "Neha",
  skills: ["HTML", "CSS", "JavaScript"]
}

Explanation:
$all is useful when you need multiple required values to be present in the same array.


Q9. Find Students with a Subject Having Marks Between 70 and 90

Problem Statement:
Find students who have at least one result where marks are between 70 and 90.

MongoDB Query:

db.students.find({
  results: {
    $elemMatch: {
      marks: {
        $gte: 70,
        $lte: 90
      }
    }
  }
})

Expected Output:

{
  name: "Aman",
  results: [
    { subject: "Python", marks: 85 },
    { subject: "SQL", marks: 72 }
  ]
}

Explanation:
$elemMatch makes sure the conditions are evaluated against the same array element.

Here, the marks must be:

70 ≤ marks ≤ 90

Q10. Find Students with Exactly 3 Skills Including Python and SQL

Problem Statement:
Find students whose skills array has exactly three elements and contains both "Python" and "SQL".

MongoDB Query:

db.students.find({
  skills: {
    $all: ["Python", "SQL"]
  },
  $expr: {
    $eq: [
      { $size: "$skills" },
      3
    ]
  }
})

Expected Output:

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

Explanation:
This query combines two checks:

  • $all → Python and SQL must exist.
  • $expr with $size → the array must contain exactly 3 elements.

This is useful for more practical array filtering.

Key Takeaways

  • MongoDB allows multiple values to be stored in an array field.
  • You can search an array directly using a value such as "skills": "Python".
  • $all checks whether an array contains all specified values.
  • $size in MongoDB checks the exact number of elements in an array.
  • $in checks whether at least one array element matches a value from a list.
  • $nin checks that array elements do not contain the specified values.
  • $elemMatch in MongoDB is useful for querying arrays containing embedded documents.
  • $elemMatch in MongoDB is especially important when multiple conditions must apply to the same array element.
  • Array queries can be combined with other MongoDB operators for more advanced filtering.

FAQs

1. How do you query an array in MongoDB?

You can directly provide a value:

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

MongoDB will find documents where "Python" exists in the skills array.

2. What is $all used for in MongoDB?

$all finds documents where an array contains all specified values.

db.students.find({
  skills: {
    $all: ["Python", "SQL"]
  }
})

3. What does $size do in MongoDB?

$size matches arrays having an exact number of elements.

db.students.find({
  skills: {
    $size: 3
  }
})

4. What is $elemMatch in MongoDB?

$elemMatch is used to find an array element that satisfies specified conditions.

For example:

db.students.find({
  results: {
    $elemMatch: {
      marks: { $gt: 80 }
    }
  }
})

5. What is the difference between $in and $all for arrays?

$in requires at least one matching value.

{ skills: { $in: ["Python", "Java"] } }

$all requires all specified values.

{ skills: { $all: ["Python", "Java"] } }

6. Can MongoDB query arrays containing objects?

Yes. $elemMatch is commonly used for this.

db.students.find({
  results: {
    $elemMatch: {
      subject: "Python",
      marks: { $gt: 80 }
    }
  }
})

7. Can multiple MongoDB operators be used with an array query?

Yes. MongoDB allows you to combine array operators with other query operators.

For example:

db.students.find({
  skills: { $all: ["Python", "SQL"] },
  $expr: {
    $eq: [{ $size: "$skills" }, 3]
  }
})

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

Scroll to Top