$range (array expression)

APPLIES TO: MongoDB vCore

The $range operator is used to generate an array of sequential integers. This operator is particularly useful for creating arrays of numbers within a specific range, which can then be used for various purposes such as pagination, indexing, or generating test data.

Syntax

The syntax for the $range operator is:

$range: [ <start>, <end>, <step> ]

Parameters

Description
start The starting value of the range (inclusive).
end The ending value of the range (exclusive).
step The increment value between each number in the range (optional, defaults to 1).

Examples

Here are some examples demonstrating the use of the $range operator.

Example 1: Generate a range of numbers

To generate an array of integers from 0 to 9:

db.stores.aggregate([
  { $match: { "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60"} }
, {
    $project: {
      rangeArray: { $range: [0, 5] }
    }
  }
])

The query results in generating an array of sequential numbers.

{
    "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
    "rangeArray": [ 0, 1, 2, 3, 4 ]
}

Example 2: Generate a range of numbers with a step value

To generate an array of even numbers from 0 to 18:

db.stores.aggregate([
  { $match: { "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60"} }
, {
    $project: {
      evenNumbers: { $range: [0, 8, 2] }
    }
  }
])

The query results in generating an array of even numbers, stepping by 2.

{
    "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60",
    "rangeArray": [ 0, 2, 4, 6 ]
}

- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore) - Read more about Feature compatibility with MongoDB.