The $cmp operator compares two values and returns -1, 0, or 1 in MongoDB aggregation pipelines. It is the building block for multi-branch logic with $switch and custom ranking.
01
Three-Way Compare
Returns -1, 0, or 1.
02
Syntax
Two values in an array.
03
Any BSON Type
Numbers, strings, dates.
04
$switch
Power categorization logic.
05
Use Cases
Sort, rank, categorize.
06
vs $gt / $lt
When to use each.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $cmp operator compares two expressions and returns a numeric result: -1 if the first is less, 0 if equal, and 1 if the first is greater. For example, $cmp(85, 80) returns 1 because 85 is greater than 80.
💡
Beginner Tip
Think of $cmp like a three-way sign: less than, equal, or greater than. Pair it with $switch or $eq to turn comparison results into labels, tiers, or custom sort keys.
Foundation
📝 Syntax
The $cmp operator takes two expressions in an array:
mongosh
{ $cmp: [ <expression1>, <expression2> ] }
Syntax Rules
$cmp — compares the first expression to the second.
$eq: [ { $cmp: [ "$score", 80 ] }, 1 ] is true when score strictly greater than 80. For scores exactly 80, use $gte instead, or add a branch for cmp === 0.
Example 3 — Compare Two Fields
Check whether actual inventory meets the expected level:
Use $cmp when you need three outcomes. Use $gte, $gt, $lte, or $lt when you only need true/false.
Applications
🚀 Use Cases
Sorting logic — build custom sort keys based on relative value order.
Categorization — assign tiers or labels by comparing values against thresholds with $switch.
Ranking — compare scores or ratings to determine relative standing.
Data validation — compare actual vs expected fields and flag mismatches.
🧠 How $cmp Works
1
MongoDB evaluates both values
The pipeline resolves both expressions — fields like "$score" or literal thresholds.
Input
2
$cmp compares them
MongoDB applies BSON comparison rules and returns -1, 0, or 1.
Compare
3
The result drives downstream logic
Use the integer in $switch, $cond, or $eq to branch your pipeline.
Output
=
⚖️
Actionable comparison data
Labels, ranks, and filters based on relative value order.
Wrap Up
Conclusion
The $cmp operator is a flexible comparison tool in MongoDB aggregation pipelines. Its three-way return value (-1, 0, 1) makes it especially powerful inside $switch for categorization and ranking logic.
For beginners, remember: 1 means greater, -1 means less, 0 means equal. For simple pass/fail checks, $gte is often easier to read than $cmp.
Use $cmp with $switch for multi-tier categorization
Remember 1 = greater, 0 = equal, -1 = less
Compare two fields when checking actual vs expected values
Use $gte when you only need a true/false result
Test edge cases: equal values and null inputs
❌ Don’t
Assume $cmp returns a boolean (it returns -1, 0, or 1)
Use cmp === 1 when you need “greater than or equal”
Compare values of incompatible BSON types without understanding order
Confuse $cmp with $eq (equality only)
Use $cmp as a query filter outside expressions
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $cmp
Use these points when comparing values in MongoDB.
5
Core concepts
⚖️01
Three-Way Result
-1, 0, or 1.
Purpose
📝02
Array Syntax
{ $cmp: [a, b] }
Syntax
📏03
1 = Greater
First > second.
Rule
🛠04
$switch Partner
Great for tiers.
Pattern
⚠05
Not Boolean
Use $gte for true/false.
Important
❓ Frequently Asked Questions
$cmp compares two values and returns -1 if the first is less, 0 if equal, or 1 if the first is greater. It follows MongoDB BSON comparison order.
The syntax is { $cmp: [ <expression1>, <expression2> ] }. Both expressions can be field references, literals, or other expressions.
-1 means the first value is less than the second. 0 means they are equal. 1 means the first value is greater than the second.
Use $gt, $lt, $gte, and $lte for simple true/false checks. Use $cmp when you need a three-way result (-1, 0, 1) for $switch branches, sorting logic, or custom comparison handling.
If either argument is null, $cmp returns null unless both are null (which returns 0).