// 1. 数据库数据
// {
//  "items": { // 集合(表名)
//      "data": [ // 数据
//          {
//            "_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
//          }
//      ]
//  }
// }

// 02. 聚合操作
'use strict';
const db = uniCloud.database();
const $ = db.command.aggregate;
exports.main = async(event, context) => {
    let res = await db.collection('items').aggregate()
        .bucketAuto({
            // 以price分组
            groupBy: '$price',
            // 分3组
            buckets: 3
        })
        .end()
    return res;
};

// 聚合之后的返回值
// {
//  "affectedDocs": 3,
//  "data": [{
//      "_id": {
//          "max": 50.3,
//          "min": 10.5
//      },
//      "count": 2
//  }, {
//      "_id": {
//          "max": 200.3,
//          "min": 50.3
//      },
//      "count": 2
//  }, {
//      "_id": {
//          "max": 200.3,
//          "min": 200.3
//      },
//      "count": 1
//  }]
// }

相关文章:

  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-07
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案