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
    //  }]
    // }
  • 相关阅读:
    Mac 配置自定义执行文件 pull.sh,push.sh
    vue-element-admin列表管理
    使用elementui图标
    Mac brew 启动php
    VUE , 表单中如何用获取接口数据的select
    Mac 如何关闭PHPstorm,双击shift快捷键
    spectacle 很好用的应用分屏工具Mac
    面对灵活的配置如何建表,使用json
    mac 下灵活管理node版本
    Node Sass version 6.0.0 is incompatible with^4.0.0
  • 原文地址:https://www.cnblogs.com/luwei0915/p/13387012.html
Copyright © 2011-2022 走看看