The $and operator combines multiple conditions with logical AND. Every expression must be true for the result to pass. Use it in find(), $match, and aggregation expressions when you need explicit multi-condition logic.
01
Logical AND
All conditions must pass.
02
Array Syntax
Pass conditions in an array.
03
$match Filters
Filter pipeline documents.
04
$expr Mode
Compare field values.
05
Use Cases
Search, eligibility, QA.
06
vs $or
AND vs OR logic.
Fundamentals
Definition and Usage
In MongoDB, the $and operator performs a logical AND on an array of expressions. In a query, documents must satisfy every condition inside $and. In an aggregation expression, $and returns true only when all sub-expressions evaluate to true.
💡
Beginner Tip
Many simple queries already use implicit AND: { status: "active", age: { $gte: 18 } }. Use explicit $and when you need multiple conditions on the same field or clearer complex logic.
Foundation
📝 Syntax
The $and operator takes an array of two or more expressions:
// Matches products with price between 100 and 500 (inclusive)
// Desk (350), Monitor (220)
How It Works
You cannot write { price: { $gte: 100, $lte: 500 } } in all contexts the same way across drivers, but range on one field works with comparison operators together. Explicit $and makes the intent clear when combining separate constraint objects.
Example 3 — $and with $expr in $match
Compare field values using aggregation expressions inside $match:
// Alice: age 25, active true, score 85 → isEligible: true
// Bob: age 17, active true, score 92 → isEligible: false
// Carol: age 30, active false, score 78 → isEligible: false
How It Works
Each sub-expression returns a boolean. $and returns true only when age, active status, and score requirements are all satisfied.
Applications
🚀 Use Cases
Product search — combine category, price range, and availability filters.
User eligibility — check age, account status, and score thresholds together.
Order validation — ensure status, amount, and budget rules all pass.
Reporting flags — add computed yes/no fields with $project and $and.
🧠 How $and Works
1
MongoDB evaluates each expression
Every condition in the $and array is checked independently.
Input
2
All must be true
If any expression fails, the whole $and fails. One false condition is enough to exclude a document or return false.
Logic
3
Result is applied
In $match, matching documents continue. In $project, the boolean is stored in a new field.
Output
=
✅
Precise multi-condition logic
Documents or computed fields reflect strict “all conditions met” rules.
Wrap Up
Conclusion
The $and operator is essential for combining multiple conditions in MongoDB. Use it in queries and aggregation pipelines when every rule must pass — especially for same-field ranges, $expr comparisons, and computed eligibility flags.
For beginners, start with simple $match filters, then move to $expr and $project as your logic grows more complex.
Use implicit AND for simple multi-field queries when possible
Use explicit $and for multiple constraints on one field
Wrap field comparisons in $match with $expr
Index fields used in $and conditions for performance
Pair with $or for mixed AND/OR logic
❌ Don’t
Overuse $and when a simple comma query is enough
Forget the array syntax around conditions
Mix query operators and aggregation expressions without $expr
Confuse $and with $allElementsTrue
Assume one failed condition still passes the document
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $and
Use these points when writing multi-condition MongoDB queries.
5
Core concepts
🔗01
Logical AND
All conditions required.
Purpose
📝02
Array Syntax
{ $and: [a, b] }
Syntax
🔍03
$match Filters
Query-stage filtering.
Usage
⚙04
$expr Mode
Field value comparisons.
Pattern
⚠05
One False Fails All
Strict AND logic.
Rule
❓ Frequently Asked Questions
$and performs a logical AND on multiple expressions. In queries, every condition must match. In aggregation expressions, it returns true only when all expressions evaluate to true.
The syntax is { $and: [ <expression1>, <expression2>, ... ] }. You must pass an array with at least two expressions.
Yes. Use $and directly in $match for field-based conditions, or wrap it inside $expr when comparing field values with expression operators.
In find() and $match, { a: 1, b: 2 } implicitly means AND. $and is explicit and useful when you need multiple conditions on the same field or complex logic.
$and combines separate boolean expressions. $allElementsTrue checks whether every element inside one array is true.