In MongoDB, the $ne
aggregation pipeline operator compares two values and returns either true
or false
, depending on whether the two values are not equivalent.
Example
Suppose we have a collection called data
with the following documents:
{ "_id" : 1, "a" : 250, "b" : 250 } { "_id" : 2, "a" : 300, "b" : 250 } { "_id" : 3, "a" : 250, "b" : 300 }
We can use the $ne
operator to compare the a
and b
fields:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 1, 2, 3 ] } } },
{
$project:
{
a: 1,
b: 1,
notEquivalent: { $ne: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "_id" : 1, "a" : 250, "b" : 250, "notEquivalent" : false } { "_id" : 2, "a" : 300, "b" : 250, "notEquivalent" : true } { "_id" : 3, "a" : 250, "b" : 300, "notEquivalent" : true }
In the first document, the a
and b
fields are equivalent, which results in a return value of false
. But in the second and third documents they are not equivalent, which results in a return value of true
.
Comparing Types
The $ne
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" : 4, "a" : 250, "b" : "250" } { "_id" : 5, "a" : "250", "b" : 250 } { "_id" : 6, "a" : 250, "b" : NumberDecimal("250") } { "_id" : 7, "a" : NumberDecimal("250"), "b" : 250 } { "_id" : 8, "a" : NumberDecimal("250"), "b" : NumberDecimal("250.00") } { "_id" : 9, "a" : NumberDecimal("250.00"), "b" : NumberDecimal("250") } { "_id" : 10, "a" : "2022-01-03T23:30:15.100Z", "b" : ISODate("2021-01-03T23:30:15.100Z") } { "_id" : 11, "a" : ISODate("2021-01-03T23:30:15.100Z"), "b" : "2021-01-03T23:30:15.100Z" }
We can apply $ne
to the a
and b
fields of those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 4, 5, 6, 7, 8, 9, 10, 11 ] } } },
{
$project:
{
a: 1,
b: 1,
isEquivalent: { $ne: [ "$a", "$b" ] }
}
}
]
).pretty()
Result:
{ "_id" : 4, "a" : 250, "b" : "250", "isEquivalent" : true } { "_id" : 5, "a" : "250", "b" : 250, "isEquivalent" : true } { "_id" : 6, "a" : 250, "b" : NumberDecimal("250"), "isEquivalent" : false } { "_id" : 7, "a" : NumberDecimal("250"), "b" : 250, "isEquivalent" : false } { "_id" : 8, "a" : NumberDecimal("250"), "b" : NumberDecimal("250.00"), "isEquivalent" : false } { "_id" : 9, "a" : NumberDecimal("250.00"), "b" : NumberDecimal("250"), "isEquivalent" : false } { "_id" : 10, "a" : "2022-01-03T23:30:15.100Z", "b" : ISODate("2021-01-03T23:30:15.100Z"), "isEquivalent" : true } { "_id" : 11, "a" : ISODate("2021-01-03T23:30:15.100Z"), "b" : "2021-01-03T23:30:15.100Z", "isEquivalent" : true }
Null Values
Comparing a value to null
returns true
, unless they’re both null
, in which case, false
is returned.
Suppose we add the following documents to the collection:
{ "_id" : 12, "a" : 250, "b" : null } { "_id" : 13, "a" : null, "b" : 250 } { "_id" : 14, "a" : null, "b" : null }
Let’s apply $ne
to those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 12, 13, 14 ] } } },
{
$project:
{
a: 1,
b: 1,
isEquivalent: { $ne: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "_id" : 12, "a" : 250, "b" : null, "isEquivalent" : true } { "_id" : 13, "a" : null, "b" : 250, "isEquivalent" : true } { "_id" : 14, "a" : null, "b" : null, "isEquivalent" : false }
Missing Fields
If one of the fields that you’re trying to compare is missing, $ne
returns true
.
Suppose we add the following documents to our collection:
{ "_id" : 15, "a" : 250 } { "_id" : 16, "b" : 250 }
Let’s apply $ne
to that document:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 15, 16 ] } } },
{
$project:
{
a: 1,
b: 1,
isEquivalent: { $ne: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "_id" : 15, "a" : 250, "isEquivalent" : true } { "_id" : 16, "b" : 250, "isEquivalent" : true }
Infinity
Comparing Infinity
to Infinity
returns false
.
Comparing -Infinity
to -Infinity
returns false
.
Comparing Infinity
to -Infinity
returns true
.
Suppose we add the following documents to our collection:
{ "_id" : 17, "a" : Infinity, "b" : Infinity } { "_id" : 18, "a" : -Infinity, "b" : -Infinity } { "_id" : 19, "a" : Infinity, "b" : -Infinity } { "_id" : 20, "a" : -Infinity, "b" : Infinity }
Let’s apply $ne
to those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 17, 18, 19, 20 ] } } },
{
$project:
{
a: 1,
b: 1,
isEquivalent: { $ne: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "_id" : 17, "a" : Infinity, "b" : Infinity, "isEquivalent" : false } { "_id" : 18, "a" : -Infinity, "b" : -Infinity, "isEquivalent" : false } { "_id" : 19, "a" : Infinity, "b" : -Infinity, "isEquivalent" : true } { "_id" : 20, "a" : -Infinity, "b" : Infinity, "isEquivalent" : true }