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.
Fundamentals
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.
When $eq returns true, $cond outputs "Tech Team". Otherwise it outputs "Business Team". This pattern is common for categorization and labeling.
Applications
🚀 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.
Wrap Up
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.
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)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $eq
Use these points when comparing values in MongoDB.
5
Core concepts
🔢01
Equality Check
Tests if values match.
Purpose
📝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.