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 $and operator do in MongoDB queries?
The $and operator combines multiple conditions and returns documents that satisfy all of them. It works like the word 'and' in everyday language, meaning all conditions must be true.
Click to reveal answer
beginner
How do you write a MongoDB query using $and to find documents where age is greater than 20 and status is 'active'?
This finds documents where both conditions are true.
Click to reveal answer
intermediate
Is it necessary to use $and explicitly when combining conditions on different fields in MongoDB?
No, MongoDB implicitly treats multiple conditions on different fields as an AND. For example, { age: { $gt: 20 }, status: 'active' } works the same as using $and.
Click to reveal answer
advanced
What happens if you use $and with an empty array in a MongoDB query?
Using $and: [] matches all documents because there are no conditions to restrict the results. It's like saying 'all conditions are true' when there are none.
Click to reveal answer
intermediate
Can $and be used to combine conditions on the same field in MongoDB? Give an example.
Yes, $and can combine multiple conditions on the same field. For example:
The $and operator requires an array of condition objects inside square brackets.
Step 2: Check each option's structure
{ $and: [ { age: { $gt: 20 } }, { city: "NY" } ] } correctly uses an array with two condition objects. Options A and B use objects instead of arrays, and C uses parentheses which is invalid.
What will the query { $and: [ { age: 25 }, { city: "LA" } ] } return?
medium
A. [{ name: "Alice", age: 25, city: "NY" }]
B. [{ name: "Bob", age: 30, city: "LA" }]
C. [{ name: "Carol", age: 25, city: "LA" }]
D. []
Solution
Step 1: Understand the query conditions
The query looks for documents where age is 25 AND city is "LA".
Step 2: Check each document against conditions
Alice has age 25 but city "NY" (fails city condition). Bob has city "LA" but age 30 (fails age condition). Carol has age 25 and city "LA" (matches both).
Hint: Both conditions must match a document to be returned [OK]
Common Mistakes:
Selecting documents matching only one condition
Ignoring the AND logic of $and
Confusing city names or ages
4.
Consider this query:
{ $and: { age: { $gt: 20 }, city: "NY" } }
What is wrong with this query?
medium
A. The operator $gt cannot be used inside $and.
B. The conditions inside $and must be in an array, not an object.
C. The city value must be a number, not a string.
D. The query is correct and will work as expected.
Solution
Step 1: Check the structure of $and
The $and operator requires an array of conditions, but here it is given an object.
Step 2: Identify the error
Using an object instead of an array causes a syntax error in MongoDB queries.
Final Answer:
The conditions inside $and must be in an array, not an object. -> Option B
Quick Check:
$and needs an array of conditions [OK]
Hint: Always use square brackets [] for $and conditions [OK]
Common Mistakes:
Using object {} instead of array [] for $and
Assuming $gt is invalid inside $and
Thinking string values are not allowed
5.
You want to find documents in a products collection where the price is greater than 100 and the category is either "electronics" or "appliances". Which query correctly uses $and and $or to achieve this?