zoukankan      html  css  js  c++  java
  • demo_10_04 云数据库聚合_group_02

    // 1. 数据库数据
    // {
    //  "avatar": { // 集合(表名)
    //      "data": [ // 数据
    //          {
    //            "_id": "1",
    //            "alias": "john",
    //            "region": "asia",
    //            "scores": [40, 20, 80],
    //            "coins": 100
    //          },
    //          {
    //            "_id": "2",
    //            "alias": "arthur",
    //            "region": "europe",
    //            "scores": [60, 90],
    //            "coins": 20
    //          },
    //          {
    //            "_id": "3",
    //            "alias": "george",
    //            "region": "europe",
    //            "scores": [50, 70, 90],
    //            "coins": 50
    //          },
    //          {
    //            "_id": "4",
    //            "alias": "john",
    //            "region": "asia",
    //            "scores": [30, 60, 100, 90],
    //            "coins": 40
    //          },
    //          {
    //            "_id": "5",
    //            "alias": "george",
    //            "region": "europe",
    //            "scores": [20],
    //            "coins": 60
    //          },
    //          {
    //            "_id": "6",
    //            "alias": "john",
    //            "region": "asia",
    //            "scores": [40, 80, 70],
    //            "coins": 120
    //          }
    //      ]
    //  }
    // }

    // 02. 聚合操作 group
    // 聚合阶段,将输入记录按给定表达式分组,输出时每个记录代表一个分组,每个记录的 _id 是区分不同组的 key。
    // 输出记录中也可以包括累计值,将输出字段设为累计值即会从该分组中计算累计值。
    'use strict';
    const db = uniCloud.database();
    const $ = db.command.aggregate;
    exports.main = async(event, context) => {
        let res = await db.collection('avatar').aggregate()
            // 按多个值分组
            // 按各个区域(region)获得相同最高分(score)的来分组,并求出各组虚拟币(coins)的总量:
            // 如果没有相同最高分的组,则单独为一组
            .group({
                // 第一个字段 _id
                _id: {
                    region: '$region',
                    maxScore: $.max('$scores')
                },
                // 第二个字段: totalCoins
                totalCoins: $.sum('$coins')
            })
            .end();
        return res;
    };

    // 聚合之后的返回值
    // {
    //  "affectedDocs": 4,
    //  "data": [{               
    //          "_id": {                    
    //              "maxScore": 20,        // "_id": "5",
    //              "region": "europe"
    //          },
    //          "totalCoins": 60
    //      },
    //      {
    //          "_id": {
    //              "maxScore": 100,      //  "_id": "4",
    //              "region": "asia"
    //          },
    //          "totalCoins": 40
    //      },
    //      {
    //          "_id": {
    //              "maxScore": 90,       // "_id": "3" + "_id": "2"  maxScore 相同 
    //              "region": "europe"
    //          },
    //          "totalCoins": 70          // totalCoins相加 20 + 50
    //      },
    //      {
    //          "_id": {
    //              "maxScore": 80,      // "_id": "1" + "_id": "6",
    //              "region": "asia"
    //          },
    //          "totalCoins": 220        // 100 + 120
    //      }
    //  ]
    // }
  • 相关阅读:
    Visual C++6.0 调用Visual Basic 6.0写的Microsoft Communications Control(ActiveX)的使用疑难及解决办法
    Associating Icons with a Category 与 恶作剧软件 有关系吗?
    WPF/Silverlight Button Styles and Templates
    Notepad++ 备忘录一
    冥思苦想,木疙瘩也能崩出个豆:扯一下各大软件的用户体验
    生活小窍门。
    Bug验证:.Net 4 下,貌似发现一个bug。如果是真,.Net组的员工该打屁股。
    两台硬件和软件配置完全相同的机器A和B,现在要用系统自带的Copy功能把A上的一个文件,复制到B上。在哪台机器上执行程序,效率更高?
    WPF 遍历 DataGrid 每行的控件
    IE ActiveX Control 和RIA
  • 原文地址:https://www.cnblogs.com/luwei0915/p/13386965.html
Copyright © 2011-2022 走看看