$eq (Equals)

Last updated: 2026-10-09

$eq (Equals)

$eq selects documents where the field value equals the specified value. It is MongoDB's most common exact-match predicate: `{ age: { $eq: 21 } }` is equivalent to `{ age: 21 }`, but $eq makes the equality intent explicit and also acts as a comparison operator in aggregation expressions. Note $eq is type-sensitive: the string "21" does not match the number 21. Also note: an explicit { $eq: /pattern/ } only matches BSON regex values, unlike implicit { field: /pattern/ } which performs regex matching.

CategoryComparison
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

fieldThe document field to match; dotted paths access nested fields (e.g. "address.city").
valueThe value to match. May be a literal or expression; sensitive to BSON type.

Returns:Cursor of matching documents

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $eq: exact match on age = 21
db.users.find({ age: { $eq: 21 } })

// Equivalent implicit equality
db.users.find({ age: 21 })

// Nested field + type sensitivity
db.users.find({ "address.city": { $eq: "Beijing" } })

Output:

// Returns all documents whose age is exactly 21 (number type)
{ "_id": ObjectId("..."), "name": "Alice", "age": 21, "address": { "city": "Beijing" } }

📌 Notes

📚 Related Tutorials

❓ FAQ

QWhat is the difference between $eq and writing { age: 21 } directly?
AFunctionally identical; $eq merely makes equality intent explicit and reads better inside $and/$or combinations.
QCan $eq match null?
AYes. $eq: null matches documents where the field is missing or null. To match only literal null, combine with $type: 10 (Null).
QHow does $eq behave on array fields?
AIt matches if any array element equals the value (equivalent to $elemMatch). For an exact whole-array match, use the array literal directly or combine with $size.
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%

🙏 帮我们做得更好

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

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