$range
The $range aggregation operator in Amazon DocumentDB is used to create an array of consecutive numbers within a specified range. This operator is particularly useful for generating sequences of numbers, such as mile markers for aid stations in a race, as demonstrated in the examples below.
Parameters
-
start: The starting value for the range. -
end: The ending value for the range. -
step: (optional) The step value to use when generating the range. If not provided, the default step value is 1.
Example (MongoDB Shell)
In this example, we'll use the $range operator to generate the mile markers for water stations in a bicycle race.
Create sample documents
db.races.insertMany([ { _id: 0, race: "STP", distance: 206 }, { _id: 1, race: "RSVP", distance: 160 }, { _id: 2, race: "Chilly Hilly", distance: 33 }, { _id: 3, race: "Flying Wheels", distance: 100 } ]);
Query example
db.races.aggregate([ { $project: { race: 1, "waterStations": { $range: [20, "$distance", 20] } } } ]);
Output
[
{
_id: 0,
race: 'STP',
waterStations: [
20, 40, 60, 80,
100, 120, 140, 160,
180, 200
]
},
{
_id: 1,
race: 'RSVP',
waterStations: [
20, 40, 60, 80,
100, 120, 140
]
},
{ _id: 2, race: 'Chilly Hilly', waterStations: [ 20 ] },
{ _id: 3, race: 'Flying Wheels', waterStations: [ 20, 40, 60, 80 ] }
]
Code examples
To view a code example for using the $range command, choose the tab for the language that you want to use: