May 20, 2021 WeChat Mini Program Development Document
Support: Small program 2.7.4, cloud function 0.8.1, Web
The aggregation phase. T he input records are grouped into different groups based on a given condition, one bucket for each group. One of the differences with buckets is that you don't need to specify boundaries, and bucketAuto automatically tries to spread records as evenly as possible across each group.
Each group is output as a record, containing a _id field with an object that contains the two fields of maximum and minimum values in the group, and a count field with the number of records in the group. count is the default output when output is not specified.
BucketAuto takes the form of:
bucketAuto({
groupBy: <expression>,
buckets: <number>,
granularity: <string>,
output: {
<output1>: <accumulator expr>,
...
<outputN>: <accumulator expr>
}
})
GroupBy is an expression that determines grouping and is applied to individual input records. Y ou can use the $ prefix plus the field path you want to group as an expression. Unless the default value is specified with default, each record needs to contain the specified field, and the field value must be within the range specified by the boundaries.
Buckets is a positive integer that specifies the number of divided groups.
Granularity is an optional enumeration value string that ensures that automatically calculated boundaries conform to a given rule. T his field can only be used if all groupBy values are numbers and there is no NaN. Enumeration values include: R5, R10, R20, R40, R80, 1-2-5, E6, E12, E24, E48, E96, E192, POWERSOF2.
Output options to determine which fields must be included in addition to _id except _id, the value of each field must be specified by the accumulator expression.When Output is specified, the default count is not output by default, you must specify manually:
output: {
count: $.sum(1),
...
<outputN>: <accumulator expr>
}
The grouping of outputs may be smaller than the given number of groups in the following cases:
Granularity is used to ensure that boundary values belong to a given sequence of numbers.
The Renard sequence is a sequence of numbers between 1.0 and 10.0 (or 10.3 if R80 is 10.3) in 5 / 10 / 20 / 40 / 80 square roots of 10.
Setting granularity to R5 / R10 / R20 / R40 / R80 limits the boundary values to the sequence. If the groupBy value is not within 1.0 to 10.0 (or 10.3 if R80 is the case), the sequence number is automatically multiplied by 10.
The E sequence is a sequence of numbers with a specific error between 1.0 and 10.0, dedded by 10's 6 / 12 / 24 / 48 / 96 / 192 squares.
The 1-2-5 sequence behaves the same as the three-value Renard sequence.
A sequence number consisting of the parties of 2.
Suppose the collection items have the following records:
{
_id: "1",
price: 10.5
}
{
_id: "2",
price: 50.3
}
{
_id: "3",
price: 20.8
}
{
_id: "4",
price: 80.2
}
{
_id: "5",
price: 200.3
}
The above records are automatically grouped into three groups:
const $ = db.command.aggregate
db.collection('items').aggregate()
.bucket({
groupBy: '$price',
buckets: 3,
})
.end()
The results are as follows:
{
"_id": {
"min": 10.5,
"max": 50.3
},
"count": 2
}
{
"_id": {
"min": 50.3,
"max": 200.3
},
"count": 2
}
{
"_id": {
"min": 200.3,
"max": 200.3
},
"count": 1
}