Skip to main content

Aggregate.end

1. Interface Description

Function: Marks the completion of the aggregation operation definition and initiates the actual aggregation operation.

Declaration: end()

2. Input Parameters

None

3. Response

ParameterTypeRequiredDescription
dataArray.<any>Requiredlist of aggregation results

4. Sample Code

const tcb = require('@cloudbase/node-sdk')
const app = tcb.init({
env: 'xxx'
})

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

exports.main = async (event, context) => {
const res = await db
.collection('books')
.aggregate()
.group({
// Group by the category field
_id: '$category',
// Make each group of output records have an avgSales field, whose value is the average of the sales field across all records in the group
avgSales: $.avg('$sales')
})
.end()
console.log(res.data)
}