MongoDB $eq Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 4 Examples
Comparison

What You’ll Learn

The $eq operator checks whether two values are equal. Use it in $match to filter documents by exact values, or in expression stages like $project and $cond to compare fields and get a true/false result.

01

Equality Check

Test if values match.

02

Two Syntax Forms

Query filter vs expression.

03

$match Stage

Filter exact matches.

04

Boolean Output

true or false in $project.

05

Use Cases

Filtering, validation, $cond.

06

Type Strict

Same BSON type required.

Definition and Usage

In MongoDB, the $eq operator tests equality in two contexts. In a $match stage, { salary: { $eq: 60000 } } filters documents where salary equals 60000. In an aggregation expression, { $eq: [ "$department", "Engineering" ] } returns true or false for each document. It is one of the most fundamental comparison tools in MongoDB.

💡
Beginner Tip

In $match, the shorthand { department: "Engineering" } is exactly the same as { department: { $eq: "Engineering" } }. Use the explicit $eq form when you need clarity or when building dynamic queries.

📝 Syntax

$eq has two forms depending on where you use it:

Query form (in $match or find filters)

mongosh
{ field: { $eq: <value> } }

// Shorthand (equivalent):
{ field: <value> }

Aggregation expression form (in $project, $cond, etc.)

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

Syntax Rules

  • Query form — filters documents where the field equals the given value.
  • Expression form — compares two expressions and returns true or false.
  • Both operands must be the same BSON type for a match (no automatic type conversion).
  • Use expression form inside $project, $addFields, $cond, and $filter.
  • Pair with $and and $or to build compound conditions.

💡 Query Form vs Expression Form

Know which form to use based on your pipeline stage:

$match{ salary: { $eq: 60000 } } (filters documents)
$project{ $eq: [ "$department", "Engineering" ] } (returns boolean)
$cond → use expression form as the if condition

⚡ Quick Reference

QuestionAnswer
Operator typeComparison operator (query + expression)
Query syntax{ field: { $eq: value } }
Expression syntax{ $eq: [ expr1, expr2 ] }
Expression outputtrue or false
Common stages$match, $project, $cond
$match filter
{
  salary: { $eq: 60000 }
}

Exact salary match

Expression
{
  $eq: [
    "$department",
    "Engineering"
  ]
}

Returns true/false

Field vs field
{
  $eq: [
    "$expected",
    "$actual"
  ]
}

Compare two fields

Shorthand
{
  status: "active"
}

Same as $eq in $match

Examples Gallery

Walk through sample employee data, filter by exact salary with $match, and compare fields with the expression form.

📚 Filter by Exact Value

Use an employees collection and find everyone with a salary of exactly 60,000 using $match.

Sample Input Documents

Suppose you have an employees collection with name, department, and salary fields:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "Alice", "department": "Engineering", "salary": 60000 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "name": "Bob", "department": "Sales", "salary": 50000 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "name": "Charlie", "department": "Engineering", "salary": 65000 }
]

Example 1 — $eq in a $match Stage

Find employees whose salary equals 60,000:

mongosh
db.employees.aggregate([
  {
    $match: {
      salary: { $eq: 60000 }
    }
  }
])

How It Works

  • Only Alice has salary: 60000, so only her document passes the filter.
  • Bob (50,000) and Charlie (65,000) are excluded.
  • The shorthand { salary: 60000 } in $match produces the same result.

📈 Practical Patterns

Use the expression form to add boolean flags, compare fields, and power conditional logic.

Example 2 — Add a Boolean Field with $project

Use the expression form to flag Engineering employees on every document:

mongosh
db.employees.aggregate([
  {
    $project: {
      name: 1,
      department: 1,
      isEngineering: {
        $eq: [ "$department", "Engineering" ]
      }
    }
  }
])

How It Works

Unlike $match, the expression form does not filter documents. It adds a true or false field to every document in the pipeline.

Example 3 — Compare Two Field Values

Check whether expected and actual values match on inventory records:

mongosh
db.inventory.aggregate([
  {
    $project: {
      item: 1,
      expected: 1,
      actual: 1,
      isMatch: {
        $eq: [ "$expected", "$actual" ]
      }
    }
  }
])

How It Works

Both operands are field references. MongoDB compares them document-by-document and returns a boolean result.

Example 4 — $eq Inside $cond

Use $eq as the condition for if-then-else logic:

mongosh
db.employees.aggregate([
  {
    $project: {
      name: 1,
      department: 1,
      teamLabel: {
        $cond: [
          { $eq: [ "$department", "Engineering" ] },
          "Tech Team",
          "Business Team"
        ]
      }
    }
  }
])

How It Works

When $eq returns true, $cond outputs "Tech Team". Otherwise it outputs "Business Team". This pattern is common for categorization and labeling.

🚀 Use Cases

  • Data filtering — select documents that match an exact field value in $match.
  • Conditional logic — use as the if condition inside $cond for categorization and labeling.
  • Data validation — compare expected vs actual fields to flag mismatches.
  • Boolean flags — add true/false fields in $project for downstream filtering or reporting.

🧠 How $eq Works

1

MongoDB reads the operands

In $match, it checks a field against a value. In expressions, it evaluates two operands like "$department" and "Engineering".

Input
2

$eq compares for equality

MongoDB checks whether both values are equal (same type and value). No type coercion is applied.

Compare
3

Result is applied in the pipeline

In $match, matching documents pass through. In expressions, true or false is stored in the output field.

Output
=

Precise matches and flags

You get filtered documents or boolean fields ready for conditional logic and validation.

Conclusion

The $eq operator is a foundational comparison tool in MongoDB. It powers exact-match filtering in $match, boolean field creation in $project, and conditional branching in $cond — making it essential for querying and data validation.

For beginners, remember two forms: query form { field: { $eq: value } } filters documents, and expression form { $eq: [ expr1, expr2 ] } returns true or false. Both require matching BSON types.

💡 Best Practices

✅ Do

  • Use query form in $match for exact-value filtering
  • Use expression form in $project and $cond
  • Remember { field: value } is shorthand for $eq
  • Ensure both operands share the same BSON type
  • Combine with $and and $or for compound filters

❌ Don’t

  • Use expression form inside $match (it won’t filter as expected)
  • Assume string "10" equals number 10
  • Forget that $eq checks exact equality, not partial matches
  • Mix up query form and expression form syntax
  • Use $eq when you need range checks (use $gte, $lt instead)

Key Takeaways

Knowledge Unlocked

Five things to remember about $eq

Use these points when comparing values in MongoDB.

5
Core concepts
📝 02

Two Forms

Query filter vs expression.

Syntax
🛠 03

$match Filter

Exact document matching.

Usage
🔍 04

$cond Ready

Boolean for if-then-else.

Use case
05

Type Strict

Same BSON type required.

Edge case

❓ Frequently Asked Questions

$eq checks whether two values are equal. In query filters it matches documents where a field equals a value. In aggregation expressions it returns true or false when comparing two expressions.
Query form: { field: { $eq: value } }. Aggregation expression form: { $eq: [ <expression1>, <expression2> ] }. The shorthand { field: value } in $match is equivalent to $eq.
In $match, $eq filters documents and keeps only matches. In $project or $addFields, $eq compares two expressions and adds a boolean field (true/false) to each document.
Yes. In aggregation expressions use { $eq: [ "$fieldA", "$fieldB" ] } to test whether two fields hold the same value on each document.
No. $eq requires both operands to be the same BSON type. Comparing a string "10" to number 10 returns false.

Continue the Operator Series

Move on to $exists to check field presence, or review $cond for conditional logic.

Next: $exists →

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