MongoDB $and Operator

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 4 Examples
Logical

What You’ll Learn

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.

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.

📝 Syntax

The $and operator takes an array of two or more expressions:

mongosh
{ $and: [ <expression1>, <expression2>, ... ] }

Query Example (in $match or find)

mongosh
{ $and: [
  { price: { $gte: 100 } },
  { inStock: true }
] }

Aggregation Expression Example (in $project)

mongosh
{ $and: [
  { $gte: [ "$age", 18 ] },
  { $eq: [ "$active", true ] }
] }

Syntax Rules

  • Array required — always wrap conditions in square brackets.
  • Minimum two expressions — otherwise use a single condition directly.
  • In $match, use field-based conditions directly inside $and.
  • For field-to-value comparisons in $match, wrap with $expr.
  • Returns false if any sub-expression is false (aggregation mode).

💡 $and vs $or vs Implicit AND

Implicit AND: { a: 1, b: 2 } — both must match
$and: all conditions must be true
$or: at least one condition must be true
Same field twice: use $and{ $and: [ { qty: { $gt: 10 } }, { qty: { $lt: 50 } } ] }

⚡ Quick Reference

QuestionAnswer
Operator typeLogical operator (query + aggregation expression)
Syntax{ $and: [ expr1, expr2, ... ] }
Minimum operands2 expressions in the array
Result (aggregation)true only if all expressions are true
Common stages$match, $project, $addFields
Query
$match: { $and: [...] }

Field-based filters

$expr
$expr: { $and: [...] }

Field comparisons

Project
isEligible: { $and }

Computed boolean

One false
[
  true,
  false
]

Returns false

Examples Gallery

Combine multiple conditions in queries, filter pipelines with $match, and compute boolean flags with $project.

📚 Query Filtering

Filter products that match multiple field conditions using $and inside $match.

Sample Input Documents

Suppose you have a products collection:

mongosh
[
  { "_id": 1, "name": "Laptop",  "price": 899,  "inStock": true,  "category": "electronics" },
  { "_id": 2, "name": "Mouse",   "price": 25,   "inStock": true,  "category": "electronics" },
  { "_id": 3, "name": "Desk",    "price": 350,  "inStock": false, "category": "furniture" },
  { "_id": 4, "name": "Monitor", "price": 220,  "inStock": true,  "category": "electronics" }
]

Example 1 — Basic $and in $match

Find in-stock electronics priced at least $100:

mongosh
db.products.aggregate([
  {
    $match: {
      $and: [
        { category: "electronics" },
        { inStock: true },
        { price: { $gte: 100 } }
      ]
    }
  },
  {
    $project: {
      name: 1,
      price: 1,
      category: 1
    }
  }
])

How It Works

  • Mouse is excluded — price is below 100.
  • Desk is excluded — not in stock and wrong category.
  • Laptop and Monitor match all three conditions.

📈 Practical Patterns

Use $expr for field comparisons and $project for eligibility flags.

Example 2 — Multiple Conditions on the Same Field

When you need two constraints on one field, explicit $and is required:

mongosh
db.products.aggregate([
  {
    $match: {
      $and: [
        { price: { $gte: 100 } },
        { price: { $lte: 500 } }
      ]
    }
  }
])

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:

mongosh
db.orders.aggregate([
  {
    $match: {
      $expr: {
        $and: [
          { $gte: [ "$total", 100 ] },
          { $lte: [ "$total", "$budget" ] },
          { $eq: [ "$status", "pending" ] }
        ]
      }
    }
  }
])

How It Works

$expr lets you use aggregation operators like $gte and $eq inside $match. $and combines them so every comparison must pass.

Example 4 — Compute an Eligibility Flag in $project

Create a boolean field without filtering documents out of the pipeline:

mongosh
db.users.aggregate([
  {
    $project: {
      name: 1,
      age: 1,
      active: 1,
      score: 1,
      isEligible: {
        $and: [
          { $gte: [ "$age", 18 ] },
          { $eq: [ "$active", true ] },
          { $gte: [ "$score", 80 ] }
        ]
      }
    }
  }
])

How It Works

Each sub-expression returns a boolean. $and returns true only when age, active status, and score requirements are all satisfied.

🚀 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.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about $and

Use these points when writing multi-condition MongoDB queries.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $anyElementTrue for “at least one true” array checks.

Next: $anyElementTrue →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful