zoukankan      html  css  js  c++  java
  • demo_10_02 云数据库聚合_bucket_02 bucketAuto

    // 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
    //  }]
    // }
  • 相关阅读:
    [Exception] 当前 TransactionScope 已完成
    C# Lazy<T>(转)
    C# 基础概念之延迟加载
    .Net语言中关于AOP 的实现详解
    DataReader、Table、DataSet和Entity相互转化
    ASP.NET Core 2.0 : 五.服务是如何加载并运行的, Kestrel、配置与环境(转)
    ASP.NET Core 2.0 : 四. _Layout与_ViewStart(转)
    ASP.NET Core 2.0 : 三. 项目结构(转)
    ASP.NET Core 2.0 : 二. 开发环境(转)
    ASP.NET Core 2.0 : 一. 概述(转)
  • 原文地址:https://www.cnblogs.com/luwei0915/p/13385798.html
Copyright © 2011-2022 走看看