MongoDB $lte Operator

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

What Is $lte?

$lte means less than or equal. Use it to filter documents where a field is at or below a value, or in aggregation to return true / false. Unlike $lt, equality does match.

Remember
$lte = less than or equal (includes equals)

Query filter:
  { score: { $lte: 80 } }

Aggregation expression:
  { $lte: [ "$score", 80 ] }  → true / false

72 ≤ 80 → true (kept / true)
80 ≤ 80 → true (kept / true)
85 ≤ 80 → false

sibling: $lt excludes equals
UsePrefer
Less than or equal$lte
Strictly less than$lt
Greater than or equal$gte

How to Use $lte in MongoDB

Pick filter mode (keep documents) or expression mode (boolean field).

  1. For filtering: use find() or $match with { field: { $lte: value } }.
  2. For a boolean: use $addFields / $project with { $lte: [ expr1, expr2 ] }.
  3. To compare two fields in a filter, wrap with $expr.
  4. Use $lt when the boundary value should not match.
PieceRequired?Role
Query form—{ field: { $lte: value } } filters docs
Expression form—{ $lte: [ a, b ] } → boolean
OperandsYesSame BSON type when possible

Pseudocode

Pseudocode
// Filter mode
for each document:
    if field <= threshold:
        keep document
    else:
        drop document

// Expression mode
for each document:
    result = (expr1 <= expr2)   // true or false
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing less-than-or-equal checks.

TopicDetail
MeaningLess than or equal (includes equals)
Query syntax{ field: { $lte: value } }
Agg syntax{ $lte: [ <a>, <b> ] }
vs $lt$lt excludes equality
Two fields in findUse $expr + $lte
TypesNumbers, dates, strings (BSON order)
Sample checkscore: { $lte: 80 } keeps 72 and 80

Live Preview

Watch $lte test whether a score is less than or equal to 80.

Simulates { score: { $lte: 80 } } / { $lte: [ score, 80 ] }.

Result
score = 80
threshold = 80
$lte → true (kept / at or below)

Worked Walkthrough — Scores ≤ 80

Keep students whose score is less than or equal to 80.

Calculation
Documents:
  { name: "Asha", score: 85 }
  { name: "Ben",  score: 72 }
  { name: "Cara", score: 80 }

Test: score ≤ 80

Asha: 85 ≤ 80 → false → drop
Ben:  72 ≤ 80 → true  → keep
Cara: 80 ≤ 80 → true  → keep  (equals match!)

Result: Ben and Cara

MongoDB Examples

We use three students, including one exactly on the boundary. First filter with a query, then add a boolean field in aggregation. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine three students: Asha (85), Ben (72), and Cara (80—exactly on the line). We save them in students.

mongosh
db.students.insertMany([
  { _id: 1, name: "Asha", score: 85 },
  { _id: 2, name: "Ben", score: 72 },
  { _id: 3, name: "Cara", score: 80 }
]);

How the Data Is Stored in MongoDB

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

mongosh
db.students.find()

Read those documents like this:

FieldAshaBenCara
_id123
name"Asha""Ben""Cara"
score857280
Picture
Threshold line at 80:

  Ben 72 ..... Cara 80 ..... Asha 85
                    |
              score ≤ 80 ?

  Ben  → yes
  Cara → yes  (equals count)
  Asha → no

How It Works

  1. Insert once. insertMany adds three student documents to students.
  2. Include the boundary. Cara’s score is exactly 80—key for $lte vs $lt.
  3. find() shows all rows. The next example keeps scores at or below 80.

Example 2 — Filter with $lte

Goal: return students whose score is less than or equal to 80.

mongosh
db.students.find({
  score: { $lte: 80 }
});

How It Works

  1. Apply a filter. { score: { $lte: 80 } } means “score must be ≤ 80”.
  2. Test each document. Ben 72 and Cara 80 pass; Asha 85 fails.
  3. Equals count. Cara stays—that is the difference from $lt.

Example 3 — Boolean Expression

Same students, but keep every document and add an atOrBelow boolean with the aggregation form of $lte.

mongosh
db.students.aggregate([
  {
    $addFields: {
      atOrBelow: {
        $lte: ["$score", 80]
      }
    }
  }
]);

How It Works

  1. Same stored documents. Still using Asha, Ben, and Cara from Example 1.
  2. Compare as an expression. { $lte: ["$score", 80] } returns a boolean per document.
  3. Nothing is dropped. All three rows stay; only the atOrBelow flag differs.

Edge Cases & Pitfalls

Check these before using $lte in production queries.

equals

Equals do match

{ score: { $lte: 80 } } includes 80. Use $lt when the boundary should fail.

types

Type mismatches surprise

Comparing a number to a string uses BSON type order, not numeric math. Keep types consistent.

fields

Two fields need $expr in find

To filter with spent ≤ budget, use $expr: { $expr: { $lte: ["$spent", "$budget"] } }.

index

Range filters love indexes

An index on the compared field helps $lte / $lt range scans stay fast on large collections.

Performance Notes

ApproachCostBest for
Indexed $lte filterRange scanLarge collections with a field index
Unindexed $lteCollection scanSmall data or rare queries
Agg boolean $ltePer-document compareFlags without dropping rows

Put selective $lte filters early in $match so later stages see fewer documents.

Key Takeaways

  • Role: less-than-or-equal comparison (includes equals).
  • Filter: { field: { $lte: value } }.
  • Expression: { $lte: [ a, b ] } → boolean.
  • vs $lt: use $lte when the boundary should match.

One line: $lte answers “is this value less than or equal to that?”—as a filter or as a boolean.

Frequently Asked Questions

$lte means less than or equal. In a query filter it keeps documents where a field is below or equal to a value. In aggregation expressions it compares two expressions and returns true or false. Equality does match.
Query form: { field: { $lte: value } }. Aggregation form: { $lte: [ <expression1>, <expression2> ] }. There is no shorthand for less-than-or-equal.
$lte includes equality. $lt is strictly less than. Score 80 matches { score: { $lte: 80 } } but not { score: { $lt: 80 } }.
Yes in aggregation: { $lte: [ "$spent", "$budget" ] }. In a find filter, compare two fields with $expr and $lte.
Yes. Numbers compare numerically, dates chronologically, and strings lexicographically. Keep operand types consistent for predictable results.
In find() / $match filters to select documents, and in expression stages like $project and $addFields to create boolean fields.

Next: $ltrim

Remove leading characters from a string in aggregation.

$ltrim tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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