Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The SDK database Aggregate division input data


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Aggregate.bucket(object: Object): Aggregate

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregation phase. The input records are divided into different groups based on a given condition and boundary, one bucket for each group.

Parameters

object: Object

Returns a value

Aggregate

API description

Each group is output as a record, containing a _id field with the following bounds as the value and a count field with the number of records in the group. count is the default output when output is not specified.

Bucket outputs only when there is at least one record in the group.

Bucket takes the form of:

bucket({
  groupBy: <expression>,
  boundaries: [<lowerbound1>, <lowerbound2>, ...],
  default: <literal>,
  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.

Boundaries is an array, each element being the lower bound of each group. A t least two boundary values must be specified. The array value must be an incremental value of the same type.

Default is optional, and after specifying, records that do not enter any grouping will enter a default grouping, the _id of which is determined by default. T he value of default must be less than the minimum value in the boundaries or greater than or equal to the maximum value in it. The value of default can be different from the value type of the boundaries element.

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>
}

Using BUCKET needs to meet at least one of the following conditions, otherwise it will throw an error:

  • Each input record application GroupBY expression is a value in Boundaries.
  • Specify a default value, which is in Boundaries, or the value different from the value of the Boundaries element.

Example

Suppose the set items have the following records:

{
  _id: "1",
  price: 10
}
{
  _id: "2",
  price: 50
}
{
  _id: "3",
  price: 20
}
{
  _id: "4",
  price: 80
}
{
  _id: "5",
  price: 200
}

The above records are grouped into groups of s0, 50, s50, s50, 100, and the others are grouped into one group:

const $ = db.command.aggregate
db.collection('items').aggregate()
  .bucket({
    groupBy: '$price',
    boundaries: [0, 50, 100],
    default: 'other',
    output: {
      count: $.sum(1),
      ids: $.push('$_id')
    }
  })
  .end()

The results are as follows:

[
  {
    "_id": 0,
    "count": 2,
    "ids": [
      "1",
      "3"
    ]
  },
  {
    "_id": 50,
    "count": 2,
    "ids": [
      "2",
      "4"
    ]
  },
  {
    "_id": "other",
    "count": 22,
    "ids": [
      "5"
    ]
  }
]