Skip to main content

db.command.aggregate.range

1. Operator Description

Description: Returns a sequence of generated numbers. Given a start value, end value, and a non-zero step, range returns a sequence starting from the start value, incrementing by the specified step, and excluding the end value.

Declaration: db.command.aggregate.range([<start>, <end>, <non-zero step>])

2. Operator Parameters

FieldTypeRequiredDescription
-<Array>ExpressionRequiredarray of aggregate expressions
FieldTypeRequiredDescription
startExpressionRequiredStart value, an expression that can be resolved to an integer
endExpressionRequiredEnd value, an expression that can be resolved to an integer
non-zero stepnumberNoOptional. Step size, an expression that can be resolved to a non-zero integer. Default is 1.

3. Sample Code

Suppose the collection stats contains the following records:

{ "_id": 1, "max": 52 }
{ "_id": 2, "max": 38 }
// Sample code in the Cloud Function environment
const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})

const db = app.database()
const $ = db.command.aggregate
const _ = db.command

exports.main = async (event, context) => {
const res = await db
.collection('stats')
.aggregate()
.project({
points: $.range([0, '$max', 10])
})
.end()
console.log(res.data)
}

The returned result is as follows:

{ "_id": 1, "points": [0, 10, 20, 30, 40, 50] }
{ "_id": 2, "points": [0, 10, 20] }