MongoDB $eq Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $eq?

$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
UsePrefer
True/false equality$eq
Not equal$ne
Three-way compare (-1 / 0 / 1)$cmp

How to Use $eq in MongoDB

Pick filter mode or boolean mode, then compare a field to a value (or two fields).

  1. To filter documents, use $match with { field: value } or { field: { $eq: value } }.
  2. To add a boolean, use $project / $addFields with { $eq: [a, b] }.
  3. Keep types aligned (string vs number are not equal).
  4. Optionally branch on the boolean with $cond.
PieceRequired?Role
$eqYesEquality test
Two expressions (agg)YesValues to compare → boolean
Field + value (query)YesKeep matching documents

Pseudocode

Pseudocode
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

Cheat Sheet

Keep these facts handy while writing equality checks.

TopicDetail
Agg syntax{ $eq: [ <a>, <b> ] }
Query syntax{ field: { $eq: value } }
Shorthand{ field: value } in find / $match
Returns (agg)true or false
TypesCompared with BSON rules (no loose coercion)
Two fields{ $eq: ["$a", "$b"] }
Sample check$eq(["$status", "open"])

Live Preview

Watch $eq test whether a status equals "open".

Simulates { $eq: ["$status", "open"] } (same idea as the mongosh examples below).

Result
status = "open"
$eq([status, "open"]) → true

{ status: "open", isOpen: true }

Worked Walkthrough — Is Status Open?

Same question for every order: does status equal the string "open"?

Calculation
Target = "open"

status "open"   → equal     → true
status "closed" → not equal → false

Aggregation field:
  isOpen: { $eq: ["$status", "open"] }

Filter form (keeps only matches):
  { $match: { status: { $eq: "open" } } }
  // same as { $match: { status: "open" } }

MongoDB Examples

We store three orders, add an isOpen boolean with $eq, then filter open orders. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine a tiny orders collection. Two orders are open; one is closed.

mongosh
db.orders.insertMany([
  { _id: 1, item: "pens",  status: "open" },
  { _id: 2, item: "pads",  status: "closed" },
  { _id: 3, item: "clips", status: "open" }
]);

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps three documents in orders. Run find() to see them:

mongosh
db.orders.find()

Read those documents like this:

Field in MongoDBValuePlain English
_id1 … 3Document id for this lesson
item"pens", "pads", "clips"Product name
status"open" or "closed"Value we will compare with $eq
Picture
Expected $eq(["$status", "open"]):

  pens  open   → true
  pads  closed → false
  clips open   → true

How It Works

  1. Insert once. insertMany adds three order documents.
  2. Statuses differ on purpose. You get both true and false results later.
  3. find() shows the same shape. Ready for $eq next.

Example 2 — Boolean with $eq

Goal: keep every document and add isOpen: true/false.

mongosh
db.orders.aggregate([
  {
    $project: {
      item: 1,
      status: 1,
      isOpen: { $eq: ["$status", "open"] },
      _id: 0
    }
  }
]);

How It Works

  1. Compare field to literal. Left is "$status"; right is "open".
  2. Get a boolean. Matching rows become true; others false.
  3. Keep all rows. $project does not drop closed orders—it only labels them.
  4. Easy check. Pads is the only false.

Example 3 — Filter with $match

Same equality idea, but keep only matching documents. Shorthand and explicit $eq are equivalent here.

mongosh
db.orders.aggregate([
  {
    $match: {
      status: { $eq: "open" }
    }
  },
  {
    $project: {
      item: 1,
      status: 1,
      _id: 0
    }
  }
]);

// Equivalent shorthand:
// { $match: { status: "open" } }

How It Works

  1. Filter first. $match keeps only documents where status equals "open".
  2. Closed is gone. Pads never reaches the next stage.
  3. Project for a short report. Keep item and status only.
  4. Shorthand tip. { status: "open" } means the same thing in $match.

Edge Cases & Pitfalls

Check these before using $eq in production pipelines.

types

No loose type coercion

"10" is not equal to 10. Align types (or convert) before comparing.

modes

Boolean vs filter

$project + $eq labels every row. $match + $eq drops non-matches. Pick the mode you mean.

null

Missing fields act like null

In aggregation expressions, a missing field compares like null. That is usually false against a string value.

vs cmp

Need less/greater? Not $eq

Use $lt / $gt for true/false ranges, or $cmp for -1 / 0 / 1.

Performance Notes

ApproachCostBest for
$match + $eq / shorthandCan use indexes on the fieldFiltering early
$eq in $projectPer-document booleanFlags without dropping rows
$eq after heavy stagesCompares more docsAvoid when you can filter first

Equality filters are cheap when they run early. Prefer $match before large projections or joins.

Key Takeaways

  • Role: equality → true / false (or keep matching docs).
  • Agg form: { $eq: [a, b] }.
  • Filter form: { field: value } shorthand in $match.
  • Types matter: "10" ≠ 10.

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.

Next: $exists

Match documents based on whether a field is present.

$exists tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful