demo_10_03 云数据库聚合_count

// 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. 聚合操作 count
// 计算上一聚合阶段输入到本阶段的记录数,输出一个记录,其中指定字段的值为记录数。
'use strict';
const db = uniCloud.database();
const $ = db.command.aggregate;
exports.main = async(event, context) => {
    let res = await db.collection('items').aggregate()
        .match({
            // 找出价格大于 50 的记录数
            price: $.gt(50)
        })
        .count('expensiveCount')
        .end();
    return res;
};

// 聚合之后的返回值
// {
//  "affectedDocs": 1,
//  "data": [{
//      "expensiveCount": 3
//  }]
// }
原文地址:https://www.cnblogs.com/luwei0915/p/13385929.html