$range - Amazon DocumentDB

$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:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); try { await client.connect(); const db = client.db('test'); const collection = db.collection('races'); const pipeline = [ { $project: { race: 1, waterStations: { $range: [20, "$distance", 20] } } } ]; const results = await collection.aggregate(pipeline).toArray(); console.dir(results, { depth: null }); } finally { await client.close(); } } example().catch(console.error);
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') try: db = client.test collection = db.races pipeline = [ { "$project": { "race": 1, "waterStations": { "$range": [20, "$distance", 20] } } } ] results = collection.aggregate(pipeline) for doc in results: print(doc) except Exception as e: print(f"An error occurred: {e}") finally: client.close() example()