Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does the $in operator do in MongoDB?
The $in operator matches documents where the value of a field equals any value in a specified array. It helps find documents with fields matching one or more values from a set.
Click to reveal answer
beginner
How would you find documents where the field color is either 'red', 'blue', or 'green'?
Use { color: { $in: ['red', 'blue', 'green'] } }. This query returns documents where the color field matches any of the listed colors.
Click to reveal answer
intermediate
Can $in be used to match multiple types of values, like numbers and strings?
Yes. $in can match any values in the array, including mixed types like numbers and strings, as long as the field value equals one of them.
Click to reveal answer
intermediate
What happens if the array passed to $in is empty?
If the array is empty, $in matches no documents because there are no values to compare against.
Click to reveal answer
intermediate
How is $in different from using multiple $or conditions?
$in is a concise way to check if a field matches any value in a list, while $or requires separate conditions for each value. $in is simpler and often more efficient.
Click to reveal answer
Which query finds documents where status is 'active' or 'pending'?
A{ status: { $eq: ['active', 'pending'] } }
B{ status: { $in: ['active', 'pending'] } }
C{ status: { $or: ['active', 'pending'] } }
D{ status: { $nin: ['active', 'pending'] } }
✗ Incorrect
$in matches if the field equals any value in the array. $eq expects a single value, $or is used differently, and $nin excludes values.
What does { age: { $in: [] } } return?
ANo documents
BDocuments where age is null
CAll documents
DDocuments where age is zero
✗ Incorrect
An empty array with $in matches no values, so no documents are returned.
Can $in match values of different types in the same query?
ANo, all values must be the same type
BOnly strings are allowed
CYes, it can match mixed types
DOnly numbers are allowed
✗ Incorrect
$in can match any values in the array, including mixed types like strings and numbers.
Which operator is a simpler alternative to multiple $or conditions checking one field?
A$in
B$and
C$not
D$exists
✗ Incorrect
$in checks if a field matches any value in a list, replacing multiple $or conditions on the same field.
If you want to find documents where tags include 'mongodb' or 'database', which query is correct?
A{ tags: { $nin: ['mongodb', 'database'] } }
B{ tags: { $eq: ['mongodb', 'database'] } }
C{ tags: { $all: ['mongodb', 'database'] } }
D{ tags: { $in: ['mongodb', 'database'] } }
✗ Incorrect
$in matches if any value in the array is present in the field. $all requires all values to be present.
Explain how the $in operator works in MongoDB queries and give a simple example.
Think about checking if a field's value is one of several options.
You got /3 concepts.
Describe the difference between using $in and multiple $or conditions for matching a set of values.
Consider how you would write queries for the same condition in two ways.
You got /3 concepts.
Practice
(1/5)
1.
What does the $in operator do in MongoDB queries?
easy
A. Deletes documents matching a condition
B. Finds documents where a field is greater than a value
C. Updates documents by adding new fields
D. Finds documents where a field matches any value in a given list
Solution
Step 1: Understand the purpose of $in
The $in operator is used to match documents where a field's value is equal to any value in a specified array.
Step 2: Compare with other options
Options B, C, and D describe different MongoDB operations unrelated to $in.
Final Answer:
Finds documents where a field matches any value in a given list -> Option D
Quick Check:
$in matches any value in list [OK]
Hint: Remember: $in checks if field is in a list [OK]
Common Mistakes:
Confusing $in with comparison operators like $gt
Thinking $in updates or deletes documents
Using $in without an array of values
2.
Which of the following is the correct syntax to find documents where the field color is either "red", "blue", or "green"?
{ color: { $in: ["red", "blue", "green"] } }
easy
A. { color: { $in: ["red", "blue", "green"] } }
B. { color: { $in: "red", "blue", "green" } }
C. { color: { $in: "red|blue|green" } }
D. { color: { $in: { "red", "blue", "green" } } }
Solution
Step 1: Check the correct use of $in syntax
The $in operator requires an array of values inside square brackets []. { color: { $in: ["red", "blue", "green"] } } correctly uses an array with strings.
Step 2: Identify syntax errors in other options
{ color: { $in: "red", "blue", "green" } } misses brackets for the array. { color: { $in: "red|blue|green" } } uses a string with pipes instead of an array. { color: { $in: { "red", "blue", "green" } } } uses curly braces which is invalid for arrays.
Filter by category in ["fruit"] returns fruit items [OK]
Hint: Match only documents with field in the given list [OK]
Common Mistakes:
Expecting documents with other categories to appear
Confusing $in with $nin (not in)
Thinking $in matches substrings instead of exact values
4.
What is wrong with this MongoDB query?
db.users.find({ age: { $in: 25, 30, 35 } })
medium
A. The query should use $nin instead of $in
B. The field name age is invalid
C. The $in operator requires an array of values, not separate arguments
D. The query is correct and will return matching documents
Solution
Step 1: Check the syntax of $in usage
The $in operator expects a single array argument containing values to match. Here, values are passed as separate arguments, which is invalid syntax.
Step 2: Identify the correct syntax
The correct syntax is { age: { $in: [25, 30, 35] } } with square brackets around the values.
Final Answer:
The $in operator requires an array of values, not separate arguments -> Option C
Quick Check:
$in needs an array [OK]
Hint: Always wrap $in values in square brackets [OK]
Common Mistakes:
Passing multiple values without an array
Confusing $in with other operators
Assuming query is valid without brackets
5.
You have a collection orders with documents containing a field status. You want to find all orders where the status is either "pending", "shipped", or "delivered" but exclude those with status "cancelled" or "returned". Which query correctly uses $in and $nin to achieve this?
Step 1: Understand the need to combine $in and $nin on the same field
We want documents where status is in one list but not in another. MongoDB does not allow combining $in and $nin inside the same object for a field. Instead, both conditions must be combined using an implicit AND by specifying both in the query object.
Step 2: Analyze each option's structure
{ status: { $in: ["pending", "shipped", "delivered"] }, status: { $nin: ["cancelled", "returned"] } } uses the same field key twice with different operators, which is invalid JSON syntax. { $or: [ { status: { $in: ["pending", "shipped", "delivered"] } }, { status: { $nin: ["cancelled", "returned"] } } ] } uses $or, which applies OR logic, not the required AND. { status: { $in: ["pending", "shipped", "delivered"] }, $nin: ["cancelled", "returned"] } places $nin outside the field, which is invalid. { status: { $in: ["pending", "shipped", "delivered"], $nin: ["cancelled", "returned"] } } tries to combine $in and $nin inside the same object, which is invalid in MongoDB.
Step 3: Correct approach
The correct way is to combine both conditions using $and or by specifying both operators inside an object using $and or by using the implicit AND by combining conditions in the query object like: { status: { $in: [...] }, status: { $nin: [...] } } is invalid JSON because keys repeat. So the correct query is: { status: { $in: [...] }, status: { $nin: [...] } } is invalid. Instead, use: { status: { $in: [...] }, status: { $nin: [...] } } is invalid. So the correct way is: { status: { $in: [...] }, $nin: [...] } is invalid. Therefore, the correct query is: { status: { $in: [...] }, $nin: [...] } is invalid. Hence, the only valid way is to use $and operator: { $and: [ { status: { $in: [...] } }, { status: { $nin: [...] } } ] }