zoukankan      html  css  js  c++  java
  • demo_10_01 云数据库聚合_addFields_01 连续两次addFields

    数据表:
    {
        "scores": { // 集合(表名)
            "data": [ // 数据
                {
                    "_id": 1,
                    "student": "Maya",
                    "homework": [10, 5, 10],
                    "quiz": [10, 8],
                    "extraCredit": 0
                },
                {
                    "_id": 2,
                    "student": "Ryan",
                    "homework": [5, 6, 5],
                    "quiz": [8, 8],
                    "extraCredit": 8
                }
            ]
        }
    }
     
    聚合操作:
    'use strict';
    const db = uniCloud.database();
    const $ = db.command.aggregate;
    exports.main = async(event, context) => {
        const msg = db.collection('scores').aggregate()
            // 添加两个字段:
            .addFields({
                totalHomework: $.sum('$homework'),
                totalQuiz: $.sum('$quiz')
            })
            // 根据上面新添加的两个字段值在添加一个字段
            .addFields({
                totalScore: $.add(['$totalHomework', '$totalQuiz', '$extraCredit'])
            })
            .end();
        return msg;
    };

    // 聚合之后的返回值
    /*

    {
        "affectedDocs": 2,
        "data": [{
            "_id": 1,
            "extraCredit": 0,
            "homework": [10, 5, 10],
            "quiz": [10, 8],
            "student": "Maya",
            "totalHomework": 25,
            "totalQuiz": 18,
            "totalScore": 43
        }, {
            "_id": 2,
            "extraCredit": 8,
            "homework": [5, 6, 5],
            "quiz": [8, 8],
            "student": "Ryan",
            "totalHomework": 16,
            "totalQuiz": 16,
            "totalScore": 40
        }]
    }

    */
  • 相关阅读:
    2.截取部分字符串中的内容(可做文件上传时的文件重命名)
    1.git fetch的使用
    2.java.lang.IllegalStateException: Optional long parameter 'id' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it ......Springmvc报错
    彩色动态球
    小球落下的动画
    form表单
    repeating-radial-gradient示例
    background示例一
    css中关于以background开的的介绍
    块元素居中的范例
  • 原文地址:https://www.cnblogs.com/luwei0915/p/13384965.html
Copyright © 2011-2022 走看看