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
Field | Type | Required | Description |
---|---|---|---|
- | <Array>Expression | Required | array of aggregate expressions |
Field | Type | Required | Description |
---|---|---|---|
start | Expression | Required | Start value, an expression that can be resolved to an integer |
end | Expression | Required | End value, an expression that can be resolved to an integer |
non-zero step | number | No | Optional. 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] }