MongoDB $lte Aggregation Pipeline Operator

In MongoDB, the $lte aggregation pipeline operator compares two values and returns either true or false, depending on whether or not the first value is less than or equal to the second value.

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 $lte operator to compare the a and b fields:

db.data.aggregate(
   [
     { $match: { _id: { $in: [ 1, 2, 3 ] } } },
     {
       $project:
          {
            _id: 0,
            a: 1,
            b: 1,
            result: { $lte: [ "$a", "$b" ] }
          }
     }
   ]
)

Result:

{ "a" : 250, "b" : 250, "result" : true }
{ "a" : 300, "b" : 250, "result" : false }
{ "a" : 250, "b" : 300, "result" : true }

In the first document, the a and b fields are equivalent, which results in a return value of true (because a is indeed less than or equal to b).

In the second document a is greater than b, which results in a return value of false (because a is not less than or equal to b).

In the third document, a is less than b and therefore the $lte operator returns true (because a is less than or equal to b).

Comparing Types

The $lte 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" }

Here’s what happens when we apply $lte 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,
            result: { $lte: [ "$a", "$b" ] }
          }
     }
   ]
).pretty()

Result:

{ "_id" : 4, "a" : 250, "b" : "250", "result" : true }
{ "_id" : 5, "a" : "250", "b" : 250, "result" : false }
{ "_id" : 6, "a" : 250, "b" : NumberDecimal("250"), "result" : true }
{ "_id" : 7, "a" : NumberDecimal("250"), "b" : 250, "result" : true }
{
	"_id" : 8,
	"a" : NumberDecimal("250"),
	"b" : NumberDecimal("250.00"),
	"result" : true
}
{
	"_id" : 9,
	"a" : NumberDecimal("250.00"),
	"b" : NumberDecimal("250"),
	"result" : true
}
{
	"_id" : 10,
	"a" : "2022-01-03T23:30:15.100Z",
	"b" : ISODate("2021-01-03T23:30:15.100Z"),
	"result" : true
}
{
	"_id" : 11,
	"a" : ISODate("2021-01-03T23:30:15.100Z"),
	"b" : "2021-01-03T23:30:15.100Z",
	"result" : false
}

Null Values

$lte comparisons can be made against null. And comparing null to null returns true.

Suppose we add the following documents to our collection:

{ "_id" : 12, "a" : 250, "b" : null }
{ "_id" : 13, "a" : null, "b" : 250 }
{ "_id" : 14, "a" : null, "b" : null }

Let’s apply $lte to those documents:

db.data.aggregate(
   [
     { $match: { _id: { $in: [ 12, 13, 14 ] } } },
     {
       $project:
          {
            _id: 0,
            a: 1,
            b: 1,
            result: { $lte: [ "$a", "$b" ] }
          }
     }
   ]
)

Result:

{ "a" : 250, "b" : null, "result" : false }
{ "a" : null, "b" : 250, "result" : true }
{ "a" : null, "b" : null, "result" : true }

Missing Fields

If one of the fields that you’re trying to compare is missing, $lte returns false if the second field is missing, and true if the first is missing.

Suppose we add the following documents to our collection:

{ "_id" : 15, "a" : 250 }
{ "_id" : 16, "b" : 250 }

Let’s apply $lte to those documents:

db.data.aggregate(
   [
     { $match: { _id: { $in: [ 15, 16 ] } } },
     {
       $project:
          {
            _id: 0,
            a: 1,
            b: 1,
            result: { $lte: [ "$a", "$b" ] }
          }
     }
   ]
)

Result:

{ "a" : 250, "result" : false }
{ "b" : 250, "result" : true }

Infinity

Comparing Infinity to Infinity returns true.

Comparing -Infinity to -Infinity returns true.

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 $lte to those documents:

db.data.aggregate(
   [
     { $match: { _id: { $in: [ 17, 18, 19, 20 ] } } },
     {
       $project:
          {
            _id: 0,
            a: 1,
            b: 1,
            result: { $lte: [ "$a", "$b" ] }
          }
     }
   ]
)

Result:

{ "a" : Infinity, "b" : Infinity, "result" : true }
{ "a" : -Infinity, "b" : -Infinity, "result" : true }
{ "a" : Infinity, "b" : -Infinity, "result" : false }
{ "a" : -Infinity, "b" : Infinity, "result" : true }