MongoDB Array Operators Practice Questions with Solutions

Introduction

MongoDB array operators are used to search and work with documents that contain array fields. Arrays can store multiple values such as skills, subjects, products, or hobbies inside a single document. In this chapter, you will practice important MongoDB array operators such as $all, $elemMatch, $size, and array-related queries using simple, practical examples. MongoDB Array Operators practice questions with solutions to help you understand the concepts.

Q1. Find Students Having Python in Their Skills

Problem Statement

Find all students whose skills array contains "Python".

MongoDB Command / Query

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

Expected Output

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

Explanation

MongoDB can directly search for a value inside an array.

If "Python" is present in the skills array, the document is returned.


Q2. Find Students Having Both Python and MongoDB

Problem Statement

Find students whose skills array contains both "Python" and "MongoDB".

MongoDB Command / Query

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

Expected Output

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

Explanation

$all is used when an array must contain all specified values.

The order of the values in the stored array does not matter.


Q3. Find Students Having Exactly 3 Skills

Problem Statement

Find students whose skills array contains exactly 3 elements.

MongoDB Command / Query

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

Expected Output

[
  {
    name: "Rahul",
    skills: ["Python", "MongoDB", "SQL"]
  },
  {
    name: "Neha",
    skills: ["HTML", "CSS", "JavaScript"]
  }
]

Explanation

$size matches arrays having the specified number of elements.

Here:

skills array length = 3

Q4. Find Students Having a Skill Array With 2 Elements

Problem Statement

Find all students whose skills array contains exactly 2 skills.

MongoDB Command / Query

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

Expected Output

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

Explanation

$size: 2 returns documents where the array contains exactly two elements.


Q5. Find Students With a Skill Matching Multiple Conditions

Problem Statement

Suppose each student has a results array containing objects with subject and marks. Find students who scored 80 or more in Python.

MongoDB Command / Query

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

Expected Output

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

Explanation

$elemMatch matches an array element when multiple conditions apply to the same element.

Here, the same result object must have:

subject = Python
AND
marks >= 80

Q6. Find Students With Either Python or Java in Skills

Problem Statement

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

MongoDB Command / Query

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

Expected Output

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

Explanation

$in checks whether an array contains at least one of the specified values.


Q7. Find Students Whose Skills Do Not Include Python

Problem Statement

Find students whose skills array does not contain "Python".

MongoDB Command / Query

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

Expected Output

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

Explanation

$nin means not in.

It returns documents where the array does not contain "Python".


Q8. Find Students Having Both HTML and CSS

Problem Statement

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

MongoDB Command / Query

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

Expected Output

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

Explanation

$all ensures that every specified value exists in the array.


Q9. Find Students With a Particular Marks Range in an Array

Problem Statement

Suppose the document contains a marks array. Find students who have at least one mark between 80 and 90, including both values.

MongoDB Command / Query

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

Expected Output

[
  {
    name: "Rahul",
    marks: [72, 85, 90]
  },
  {
    name: "Neha",
    marks: [78, 82, 95]
  }
]

Explanation

$elemMatch checks whether at least one array element satisfies the specified conditions.

Here, at least one mark must satisfy:

80 <= mark <= 90

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

Problem Statement

Find students whose skills array contains exactly 3 elements and includes both "Python" and "SQL".

MongoDB Command / Query

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

Expected Output

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

Explanation

This query combines two conditions:

  1. $all checks that Python and SQL are present.
  2. $expr with $size checks that the array has exactly 3 elements.

So the student must have exactly three skills, including Python and SQL.

Key Takeaways

  • MongoDB can directly search for values inside arrays.
  • $all checks whether an array contains all specified values.
  • $size matches an array with an exact number of elements.
  • $elemMatch is useful when multiple conditions must match the same array element.
  • $in can find documents where an array contains at least one specified value.
  • $nin can exclude documents containing specified array values.
  • $elemMatch works with arrays of values as well as arrays of embedded documents.
  • $all does not require the values to appear in a particular order.
  • $size checks the exact array length.
  • Array operators are useful for skills, subjects, products, tags, marks, hobbies, and many other types of data.

FAQs

1. What are array operators in MongoDB?

Array operators are MongoDB query operators used to search and work with fields containing arrays. Common examples include $all, $elemMatch, and $size.

2. What is the use of $all in MongoDB?

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

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

3. What does $size do in MongoDB?

$size matches arrays that contain an exact number of elements.

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

This finds documents where skills contains exactly three elements.

4. What is $elemMatch in MongoDB?

$elemMatch is used to match array elements against multiple conditions.

For example:

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

This finds documents having at least one mark between 80 and 90.

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

$all requires all specified values to be present in the array, while $in requires at least one specified value to match.

$all → Python AND MongoDB
$in  → Python OR MongoDB

6. Can MongoDB search directly inside an array?

Yes. MongoDB allows direct matching against array elements.

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

This finds documents where "Python" is an element of the skills array.

7. Can $elemMatch be used with an array of objects?

Yes. $elemMatch is especially useful for arrays containing embedded documents.

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

It ensures that both conditions apply to the same array element.

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

Scroll to Top