$eq (Equals)
Last updated: 2026-10-09
$eq (Equals)
$eq is the expression-form equality comparison, taking two arguments and returning a boolean. It runs in expression contexts such as $project or $expr inside $match, and differs from the query operator: queries write { field: { $eq: value } } while expressions write { $eq: [expr1, expr2] }. It is used to compare fields with each other or to drive $cond conditions. Comparisons follow BSON type ordering, and null equals null.
| Category | Set, Condition & Conversion |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $eq: [ <expression1>, <expression2> ] }
⚙️ Parameters
| expression 1 | The first expression to compare. |
|---|---|
| expression 2 | The second expression to compare. |
Returns:true when the values are equal, false otherwise
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $eq: check for free shipping
db.orders.aggregate([
{ $project: {
freeShip: { $eq: ['$shipCost', 0] }
} }
])
Output:
{ '_id': 1, 'freeShip': true }
{ '_id': 2, 'freeShip': false }
📌 Notes
- Expression context uses an array form; query context uses field plus value
- null compared with null returns true
- Comparisons follow the BSON type ordering
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QHow does the $eq syntax differ between expressions and queries?
AQueries write { field: { $eq: value } }; expressions write { $eq: [expr1, expr2] }.
QHow does $eq compare null?
Anull equals null and differs from every other type.
QCan $eq compare arrays?
AYes, element by element; different order means not equal.