Aggregate.bucket
1. Interface Description
Function: Aggregation Stage. Divides input records into different groups based on given conditions and boundaries, with each group being a bucket.
Disclaimer:
bucket({ groupBy: <expression>, boundaries: [<lowerbound1>, <lowerbound2>, ...], default: <literal>, output: { <output1>: <accumulator expr>, ... <outputN>: <accumulator expr> } })
Notes:
Each group is output as a record, containing an
_idfield with the value of the lower bound and acountfield with the value of the number of records in the group.countis output by default whenoutputis not specified.
bucketis output only when there is at least one record in the group.
To use
bucket, at least one of the following conditions must be met; otherwise, an error will be thrown:
- For every input record, the value obtained by applying the
groupByexpression must be withinboundaries. - Specify a
defaultvalue that is outsideboundariesor has a different type than the values inboundaries.
2. Input Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| groupBy | expression | Yes | The field is detailed below |
| boundaries | Array | Yes | The field is detailed below |
| default | any | No | The field is detailed below |
| output | Object | No | The field is detailed below |
groupByis an expression used to determine grouping, applied to each input record. You can use a$prefix followed by the field path to group as the expression. Unless a default value is specified withdefault, each record must contain the specified field, and the field value must be within the range specified byboundaries.
boundariesis an array where each element represents the lower bound of a group. At least two boundary values must be specified. The array values must be of the same type and in ascending order.
defaultis optional. When specified, records that do not fall into any group will be placed into a default group, where the_idof this group is determined bydefault. The value ofdefaultmust be less than the minimum value inboundariesor greater than or equal to its maximum value. The value ofdefaultcan be of a different type than the elements inboundaries.
outputis optional, used to determine which fields, in addition to_id, the output records should contain. The value of each field must be specified using an accumulator expression. Whenoutputis specified, the defaultcountis not output by default and must be manually specified:
output: {
count: $.sum(1),
...
<outputN>: <accumulator expr>
}
3. Response
| Parameter | Type | Required | Description |
|---|---|---|---|
| - | Aggregate | Yes | Aggregation object |
4. Sample Code
Suppose the collection items contains the following records:
{
_id: "1",
price: 10
}
{
_id: "2",
price: 50
}
{
_id: "3",
price: 20
}
{
_id: "4",
price: 80
}
{
_id: "5",
price: 200
}
Group the above records, categorizing [0, 50) into one group, [50, 100) into another group, and others into a separate group:
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("items")
.aggregate()
.bucket({
groupBy: "$price",
boundaries: [0, 50, 100],
default: "other",
output: {
count: $.sum(),
ids: $.push("$_id"),
},
})
.end();
console.log(res.data);
};
The returned result is as follows:
[
{
"_id": 0,
"count": 2,
"ids": [
"1",
"3"
]
},
{
"_id": 50,
"count": 2,
"ids": [
"2",
"4"
]
},
{
"_id": "other",
"count": 1,
"ids": [
"5"
]
}
]