zoukankan      html  css  js  c++  java
  • demo_10_05 云数据库聚合_limit

    // 1. 数据库数据
    // {
    //  "items": { // 集合(表名)
    //      "data": [ // 数据
    //          {
    //              "_id": "1",
    //              "price": 10
    //          },
    //          {
    //              "_id": "2",
    //              "price": 50
    //          },
    //          {
    //              "_id": "3",
    //              "price": 20
    //          },
    //          {
    //              "_id": "4",
    //              "price": 80
    //          },
    //          {
    //              "_id": "5",
    //              "price": 200
    //          }
    //      ]
    //  }
    // }

    // 02. 聚合操作 limit
    // 聚合阶段,限制输出到下一阶段的记录数。
    'use strict';
    const db = uniCloud.database();
    const $ = db.command.aggregate;
    exports.main = async(event, context) => {
        let res = await db.collection('items').aggregate()
            // 返回价格大于 20 的记录的最小的两个记录:
            .match({
                price: $.gt(20)
            })
            .sort({
                price: 1,
            })
            .limit(2)
            .end();
        return res;
    };

    // 聚合之后的返回值
    // {
    //  "affectedDocs": 2,
    //  "data": [{
    //      "_id": "2",
    //      "price": 50
    //  }, {
    //      "_id": "4",
    //      "price": 80
    //  }]
    // }
  • 相关阅读:
    Mysql热备份
    win10 上安装虚拟机
    SpringMVC AJAX向后台传递数组参数/实体集合
    解决eclipse中tomcat不加载web项目的问题
    Python 基础第九天
    Python 基础第8天(文件管理)
    Python 基础第七天
    Python 基础第六天
    Python 基础第五天
    Python 基础第四天
  • 原文地址:https://www.cnblogs.com/luwei0915/p/13387012.html
Copyright © 2011-2022 走看看