The $and operator helps you find documents that match all given conditions at the same time.
$and operator behavior in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
{ $and: [ { condition1 }, { condition2 }, ... ] }Each condition inside the array is a separate filter.
All conditions must be true for a document to match.
{ $and: [ { age: { $gt: 30 } }, { city: 'New York' } ] }{ $and: [ { price: { $lt: 20 } }, { inStock: true } ] }{ $and: [ { author: 'Alice' }, { year: { $gte: 2010 } } ] }This query finds all products in the 'electronics' category with a price less than 100.
db.products.find({ $and: [ { category: 'electronics' }, { price: { $lt: 100 } } ] })If you only have one condition, you don't need $and because MongoDB treats it as AND by default.
Using $and is helpful when you want to combine multiple complex conditions explicitly.
$and finds documents matching all listed conditions.
Conditions are inside an array and all must be true.
Use it to combine filters that must happen together.
Practice
What does the $and operator do in a MongoDB query?
Solution
Step 1: Understand the purpose of
The$and$andoperator combines multiple conditions and requires all to be true for a document to match.Step 2: Compare with other operators
Unlike$or, which matches if any condition is true,$andneeds all conditions true.Final Answer:
It finds documents that match all the given conditions. -> Option AQuick Check:
$andmeans all conditions must match [OK]
- Confusing $and with $or operator
- Thinking $and sorts documents
- Assuming $and deletes documents
Which of the following is the correct syntax to use $and in a MongoDB query?
{ $and: [ { age: { $gt: 20 } }, { city: "NY" } ] }Solution
Step 1: Recall the syntax of
The$and$andoperator 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.Final Answer:
{ $and: [ { age: { $gt: 20 } }, { city: "NY" } ] } -> Option AQuick Check:
$andneeds an array of conditions [OK]
- Using curly braces {} instead of array []
- Using parentheses () instead of array []
- Putting conditions directly without array
Given the collection users with documents:
[{ name: "Alice", age: 25, city: "NY" }, { name: "Bob", age: 30, city: "LA" }, { name: "Carol", age: 25, city: "LA" }]What will the query { $and: [ { age: 25 }, { city: "LA" } ] } return?
Solution
Step 1: Understand the query conditions
The query looks for documents whereageis 25 ANDcityis "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).Final Answer:
[{ name: "Carol", age: 25, city: "LA" }] -> Option CQuick Check:
Both conditions true only for Carol [OK]
- Selecting documents matching only one condition
- Ignoring the AND logic of $and
- Confusing city names or ages
Consider this query:
{ $and: { age: { $gt: 20 }, city: "NY" } }What is wrong with this query?
Solution
Step 1: Check the structure of $and
The$andoperator 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 BQuick Check:
$andneeds an array of conditions [OK]
- Using object {} instead of array [] for $and
- Assuming $gt is invalid inside $and
- Thinking string values are not allowed
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?
Solution
Step 1: Understand the conditions
The query needs price > 100 AND category is either "electronics" OR "appliances".Step 2: Check each option's logic
{ $and: [ { price: { $gt: 100 } }, { $or: [ { category: "electronics" }, { category: "appliances" } ] } ] } correctly uses$andwith price condition and an inner$orfor categories. { $or: [ { price: { $gt: 100 } }, { category: "electronics" }, { category: "appliances" } ] } uses$orfor all, which is incorrect. { price: { $gt: 100 }, category: { $or: [ "electronics", "appliances" ] } } uses invalid syntax for$orinside category. { $and: { price: { $gt: 100 }, category: { $in: [ "electronics", "appliances" ] } } } uses$andwith an object instead of array, which is invalid.Final Answer:
{ $and: [ { price: { $gt: 100 } }, { $or: [ { category: "electronics" }, { category: "appliances" } ] } ] } -> Option DQuick Check:
Combine $and for price and $or for categories [OK]
- Using $or for all conditions instead of $and
- Incorrect syntax for $or inside category
- Using object instead of array for $and
