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

The SDK database Aggregate initiates the actual aggregation operation


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Aggregate.end(): Promise<Object>

Support: Small program 2.7.4, cloud function 0.8.1, Web

Flag aggregation operation definition is complete, initiate the actual aggregation operation

Returns a value

Promise.<Object>

Property Type Description
list Array.<any> Aggregate the list of results

The sample code

const $ = db.command.aggregate
db.collection('books').aggregate()
  .group({
    // 按 category 字段分组
    _id: '$category',
    // 让输出的每组记录有一个 avgSales 字段,其值是组内所有记录的 sales 字段的平均值
    avgSales: $.avg('$sales')
  })
  .end()
  .then(res => console.log(res))
  .catch(err => console.error(err))

The small terminal is compatible with callback styles

const $ = db.command.aggregate
db.collection('books').aggregate()
  .group({
    // 按 category 字段分组
    _id: '$category',
    // 让输出的每组记录有一个 avgSales 字段,其值是组内所有记录的 sales 字段的平均值
    avgSales: $.avg('$sales')
  })
  .end({
    success: function(res) {
      console.log(res)
    },
    fail: function(err) {
      console.error(err)
    }
  })