$eq is MongoDB’s equality operator. In aggregation it compares two expressions and returns true or false. In query filters it keeps documents where a field equals a value.
Remember
$eq = equal? → true / false
Aggregation:
{ $eq: [ <a>, <b> ] }
Query / $match:
{ field: { $eq: value } }
{ field: value } // shorthand
$eq(["$status", "open"]) → true or false
"10" vs 10 → false // type matters
for each document:
a = evaluate(expression1) // missing → like null
b = evaluate(expression2)
if a and b are equivalent (value + type):
result = true
else:
result = false
emit { ..., field: result } // or keep/drop doc in $match
Reference
Cheat Sheet
Keep these facts handy while writing equality checks.
Topic
Detail
Agg syntax
{ $eq: [ <a>, <b> ] }
Query syntax
{ field: { $eq: value } }
Shorthand
{ field: value } in find / $match
Returns (agg)
true or false
Types
Compared with BSON rules (no loose coercion)
Two fields
{ $eq: ["$a", "$b"] }
Sample check
$eq(["$status", "open"])
Try it
Live Preview
Watch $eq test whether a status equals "open".
Simulates { $eq: ["$status", "open"] } (same idea as the mongosh examples below).
One line:$eq answers “are these two values the same?”—as a boolean, or as a document filter.
Frequently Asked Questions
$eq checks whether two values are equal. In aggregation expressions it returns true or false. In query filters it matches documents where a field equals a value.
Aggregation: { $eq: [ <expression1>, <expression2> ] }. Query filter: { field: { $eq: value } }. In $match, the shorthand { field: value } is equivalent to $eq.
Yes. $eq compares value and type using BSON comparison rules. The string "10" is not equal to the number 10.
Yes. In aggregation use { $eq: [ "$fieldA", "$fieldB" ] } to test whether two fields hold the same value on each document.
Use $eq for a true/false equality check. Use $cmp when you need a three-way result (-1, 0, or 1).
In aggregation expressions, a missing field is treated like null. $eq([null, null]) is true. $eq(["$missing", "open"]) is false.