Skip to main content

Aggregate.group

1. Interface Description

Function: Groups input records by a given expression, with each output record representing a group and each record's _id serving as the key distinguishing different groups. Output records can also include accumulated values, where setting an output field to an accumulated value calculates it from the group.

Syntax: group({ _id: <expression>, <field1>: <accumulator1>, ... <fieldN>: <accumulatorN> })

2. Input Parameters

ParameterTypeRequiredDescription
_idexpressionYesis the key used to distinguish different groups
fieldNexpressionNofieldN: These additional fields are optional and represent accumulated values. Use accumulators like $.sum, but other expressions can also be employed.

The accumulator must be one of the following operators:

  • addToSet
  • avg
  • first
  • last
  • max
  • min
  • push
  • stdDevPop
  • stdDevSamp
  • sum

Memory Limit

This stage has a 100M memory usage limit.

3. Response

ParameterTypeRequiredDescription
-AggregateYesAggregation object

4. Sample Code

Grouping by Field Value

Suppose the collection avatar contains the following records:

{
_id: "1",
alias: "john",
region: "asia",
scores: [40, 20, 80],
coins: 100
}
{
_id: "2",
alias: "arthur",
region: "europe",
scores: [60, 90],
coins: 20
}
{
_id: "3",
alias: "george",
region: "europe",
scores: [50, 70, 90],
coins: 50
}
{
_id: "4",
alias: "john",
region: "asia",
scores: [30, 60, 100, 90],
coins: 40
}
{
_id: "5",
alias: "george",
region: "europe",
scores: [20],
coins: 60
}
{
_id: "6",
alias: "john",
region: "asia",
scores: [40, 80, 70],
coins: 120
}
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("avatar")
.aggregate()
.group({
_id: "$alias",
num: $.sum(1),
})
.end();
console.log(res.data);
};

The returned result is as follows:

{
"_id": "john",
"num": 3
}
{
"_id": "authur",
"num": 1
}
{
"_id": "george",
"num": 2
}

Grouping by Multiple Values

You can group by multiple values by passing a record to _id. Still using the example data from above, group by region with the same highest score, and calculate the total coins amount for each group.

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("avatar")
.aggregate()
.group({
_id: {
region: "$region",
maxScore: $.max("$scores"),
},
totalCoins: $.sum("$coins"),
})
.end();
console.log(res.data);
};

The returned result is as follows:

{
"_id": {
"region": "asia",
"maxScore": 80
},
"totalCoins": 220
}
{
"_id": {
"region": "asia",
"maxScore": 100
},
"totalCoins": 100
}
{
"_id": {
"region": "europe",
"maxScore": 90
},
"totalCoins": 70
}
{
"_id": {
"region": "europe",
"maxScore": 20
},
"totalCoins": 60
}