zoukankan      html  css  js  c++  java
  • demo_10_01 云数据库聚合_addFields_02

    // 1. 数据库数据
    // {
    //  "vehicles": { // 集合(表名)
    //      "data": [ // 数据
    //          {
    //              "_id": 1,
    //              "type": "car",
    //              "specs": {
    //                  "doors": 4,
    //                  "wheels": 4
    //              }
    //          },
    //          {
    //              "_id": 2,
    //              "type": "motorcycle",
    //              "specs": {
    //                  "doors": 0,
    //                  "wheels": 2
    //              }
    //          },
    //          {
    //              "_id": 3,
    //              "type": "jet ski"
    //          }
    //      ]
    //  }
    // }

    // 02. 聚合操作
    'use strict';
    const db = uniCloud.database();
    const $ = db.command.aggregate;
    exports.main = async(event, context) => {
        /* 示例1:在嵌套记录里增加字段
        
        const msg = db.collection('vehicles').aggregate()
            .addFields({
                // 在已有的集合里添加字段,而不是创建新的集合
                'specs.fuel_type': 'unleaded'
            })
            .end();
            
        */

        // 示例2:设置字段值为另一个字段
        const msg = db.collection('vehicles').aggregate()
            // 添加了一个新字段
            .addFields({
                vehicle_type: '$type'
            })
            .end()

        return msg;
    };

    // 聚合之后的返回值
    /* 示例1:

    {
        "affectedDocs": 3,
        "data": [{
            "_id": 1,
            "specs": {
                "doors": 4,
                "fuel_type": "unleaded",
                "wheels": 4
            },
            "type": "car"
        }, {
            "_id": 2,
            "specs": {
                "doors": 0,
                "fuel_type": "unleaded",
                "wheels": 2
            },
            "type": "motorcycle"
        }, {
            "_id": 3,
            "specs": {
                "fuel_type": "unleaded"
            },
            "type": "jet ski"
        }]
    }

    */

    /* 示例2:

    {
        "affectedDocs": 3,
        "data": [{
            "_id": 1,
            "specs": {
                "doors": 4,
                "wheels": 4
            },
            "type": "car",
            "vehicle_type": "car"
        }, {
            "_id": 2,
            "specs": {
                "doors": 0,
                "wheels": 2
            },
            "type": "motorcycle",
            "vehicle_type": "motorcycle"
        }, {
            "_id": 3,
            "type": "jet ski",
            "vehicle_type": "jet ski"
        }]
    }

    */
  • 相关阅读:
    什么是“泛在电力物联网”?要建一个什么样的泛在电力物联网?
    基于混合云雾计算的物联网架构
    探索 | “中医+AI”会诊电力设备故障
    泛在电力物联网有项核心技术 你听过没有?
    国网做泛在电力物联网的初衷是什么?如何参与?
    泛在电力物联网技术及战略解读:一个战略 两个领域 三个阶段
    构建“泛在电力物联网”成为国网当前最紧迫、最重要的任务
    如何解决分布式日志exceptionless的写入瓶颈
    SQL 查找是否"存在",别再 count 了,很耗费时间的
    abp vnext 微服务-基于Exceptionless实现分布式日志记录
  • 原文地址:https://www.cnblogs.com/luwei0915/p/13385113.html
Copyright © 2011-2022 走看看