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

CategorySet, Condition & Conversion
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

expression 1The first expression to compare.
expression 2The 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

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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏