In MongoDB, the $eq
aggregation pipeline operator compares two values and returns either true
or false
, depending on whether the two values are equivalent or not.
Example
Suppose we have a collection called data
with the following documents:
{ "_id" : 1, "a" : 250, "b" : 250 } { "_id" : 2, "a" : 250, "b" : 100 }
We can use the $eq
operator to compare the a
and b
fields:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 1, 2 ] } } },
{
$project:
{
a: 1,
b: 1,
isEquivalent: { $eq: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "_id" : 1, "a" : 250, "b" : 250, "isEquivalent" : true } { "_id" : 2, "a" : 250, "b" : 100, "isEquivalent" : false }
In the first document, the a
and b
fields are equivalent, which results in a return value of true
. But in the second document they are not equivalent, which results in a return value of false
.
Comparing Types
The $eq
operator compares both value and type using the specified BSON comparison order for values of different types.
Suppose our collection contains the following documents:
{ "_id" : 3, "a" : 250, "b" : "250" } { "_id" : 4, "a" : 250, "b" : NumberDecimal("250") } { "_id" : 5, "a" : NumberDecimal("250"), "b" : NumberDecimal("250.00") } { "_id" : 6, "a" : "2021-01-03T23:30:15.100Z", "b" : ISODate("2021-01-03T23:30:15.100Z") }
We can apply $eq
to the a
and b
fields of those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 3, 4, 5, 6 ] } } },
{
$project:
{
a: 1,
b: 1,
isEquivalent: { $eq: [ "$a", "$b" ] }
}
}
]
).pretty()
Result:
{ "_id" : 3, "a" : 250, "b" : "250", "isEquivalent" : false } { "_id" : 4, "a" : 250, "b" : NumberDecimal("250"), "isEquivalent" : true } { "_id" : 5, "a" : NumberDecimal("250"), "b" : NumberDecimal("250.00"), "isEquivalent" : true } { "_id" : 6, "a" : "2021-01-03T23:30:15.100Z", "b" : ISODate("2021-01-03T23:30:15.100Z"), "isEquivalent" : false }
In document 3, both a
and b
have a value of 250
, but if you look closely at b
, it’s a string (it’s surrounded by double quotes). Therefore, the first document returns false
.
Document 4 returns true
because both fields contain 250
and both are numbers. This is true even though a
is a double and b
is a decimal.
Document 5 returns true
because both are decimals and both are the same value (even though one explicitly uses decimal places and the other doesn’t).
Document 6 returns false
because, even though the date/time value is exactly the same, they use different types to express that date (a
uses a date/time string and b
uses a Date object).
Null Values
Comparing a value to null
returns false
, unless they’re both null
.
Suppose we add the following documents to our collection:
{ "_id" : 7, "a" : 250, "b" : null } { "_id" : 8, "a" : null, "b" : null }
Let’s apply $eq
to those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 7, 8 ] } } },
{
$project:
{
a: 1,
b: 1,
isEquivalent: { $eq: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "_id" : 7, "a" : 250, "b" : null, "isEquivalent" : false } { "_id" : 8, "a" : null, "b" : null, "isEquivalent" : true }
Missing Fields
If one of the fields that you’re trying to compare is missing, $eq
returns false
.
Suppose we add the following document to our collection:
{ "_id" : 9, "a" : 250 }
Let’s apply $eq
to that document:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 9 ] } } },
{
$project:
{
a: 1,
b: 1,
isEquivalent: { $eq: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "_id" : 9, "a" : 250, "isEquivalent" : false }
Infinity
Comparing Infinity
to Infinity
returns true
.
Comparing -Infinity
to -Infinity
returns true
.
Comparing Infinity
to -Infinity
returns false
.
Suppose we add the following documents to our collection:
{ "_id" : 10, "a" : Infinity, "b" : Infinity } { "_id" : 11, "a" : -Infinity, "b" : -Infinity } { "_id" : 12, "a" : Infinity, "b" : -Infinity }
Let’s apply $eq
to those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 10, 11, 12 ] } } },
{
$project:
{
a: 1,
b: 1,
isEquivalent: { $eq: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "_id" : 10, "a" : Infinity, "b" : Infinity, "isEquivalent" : true } { "_id" : 11, "a" : -Infinity, "b" : -Infinity, "isEquivalent" : true } { "_id" : 12, "a" : Infinity, "b" : -Infinity, "isEquivalent" : false }