The $range operator generates an array of integers in sequence inside MongoDB aggregation pipelines. Use it to create index lists, month numbers, pagination slots, or any counted sequence without storing it in your data.
01
Number Sequence
Start to end array.
02
Start / End
Inclusive start, exclusive end.
03
Step Value
Skip by 2, -1, etc.
04
+ $unwind
One row per index.
05
Use Cases
Months, pages, loops.
06
+ $map
Indexed transforms.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $range operator returns an array of integers from a start value up to (but not including) an end value. For example, { $range: [0, 5] } produces [0, 1, 2, 3, 4], and { $range: [0, 10, 2] } produces [0, 2, 4, 6, 8].
This is especially useful when you need a sequence on the fly — calendar months, page numbers, retry counts, or array indexes — without pre-storing those values in documents.
💡
Beginner Tip
Think of $range like Python’s range(start, end, step) or JavaScript’s loop counter. The end is exclusive, so to get 1 through 5 use { $range: [1, 6] }.
Foundation
📝 Syntax
The $range operator takes an array with two or three expressions:
$range creates [1, 2, 3, 4, 5]; $map transforms each value. Pair $range with $map or $reduce for indexed logic.
Applications
🚀 Use Cases
Pagination indexes — generate page numbers from a total count field.
Calendar expansion — produce month or day sequences for reporting pipelines.
Test data — create indexed rows without inserting sequence documents.
Array iteration — feed $map or $reduce with a controlled index range.
🧠 How $range Works
1
MongoDB reads start, end, and step
The pipeline evaluates the three expressions — literals like 0 and 5, or field references like "$pageCount".
Input
2
$range builds the sequence
MongoDB increments (or decrements) by step from start until the next value would reach or pass end.
Generate
3
An integer array is returned
The array is stored in your projected field or passed to $unwind, $map, or $zip.
Output
=
🔢
On-demand sequences
Generate indexes, months, or loops without extra collection data.
Wrap Up
Conclusion
The $range operator generates integer sequences in MongoDB aggregation pipelines. With inclusive start, exclusive end, and an optional step, it replaces many hand-built index arrays for pagination, calendars, and iteration.
Remember end is exclusive and step cannot be zero. Pair with $unwind for one-row-per-index output. Next in the series: $reduce.
Pass field references for dynamic lengths: "$pageCount"
Combine with $map for indexed transformations
❌ Don’t
Use step 0 (MongoDB errors)
Assume end is inclusive
Use positive step when start > end (returns empty array)
Generate huge ranges that could exhaust memory
Confuse $range with query $gte/$lte filters
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $range
Use these points when generating sequences in aggregation pipelines.
5
Core concepts
🔢01
Integer Sequence
Start to end array.
Purpose
📝02
Exclusive End
Stops before end.
Syntax
🛠03
Step Default 1
Non-zero required.
Rule
🔀04
+ $unwind
One row per index.
Pattern
📅05
Months 1–12
[1, 13]
Use case
❓ Frequently Asked Questions
$range generates an array of integers in a sequence. You specify a start value (inclusive), an end value (exclusive), and an optional step. For example, { $range: [0, 5] } returns [0, 1, 2, 3, 4].
The syntax is { $range: [ <start>, <end>, <optional non-zero step> ] }. Start is inclusive, end is exclusive, and step defaults to 1 if omitted.
The end value is exclusive. { $range: [0, 5] } stops before 5 and returns [0, 1, 2, 3, 4]. To include 5, use end: 6.
Yes. Use a negative step when start is greater than end. For example, { $range: [5, 0, -1] } returns [5, 4, 3, 2, 1]. The step cannot be zero.
Generate a sequence in $project or $addFields, then $unwind the array to create one document per index. This is useful for pagination slots, repeated rows, or indexed iteration.