zoukankan      html  css  js  c++  java
  • demo_10_06 云数据库聚合_lookup_01

    // 1. 数据库数据
    // {
    //  "orders": { // 集合(表名)
    //      "data": [ // 数据
    //          {"_id":4,"book":"novel 1","price":30,"quantity":2},
    //          {"_id":5,"book":"science 1","price":20,"quantity":1},
    //          {"_id":6}
    //      ]
    //  },
    //  "books": { // 集合(表名)
    //      "data": [ // 数据
    //          {"_id":"book1","author":"author 1","category":"novel","stock":10,"time":1564456048486,"title":"novel 1"},
    //          {"_id":"book3","author":"author 3","category":"science","stock":30,"title":"science 1"},
    //          {"_id":"book4","author":"author 3","category":"science","stock":40,"title":"science 2"},
    //          {"_id":"book2","author":"author 2","category":"novel","stock":20,"title":"novel 2"},
    //           {"_id":"book5","author":"author 4","category":"science","stock":50,"title":null},
    //          {"_id":"book6","author":"author 5","category":"novel","stock":"60"}
    //      ]
    //  }
    // }


    // 02. 聚合操作 lookup
    // 聚合阶段,联表查询。与同个数据库下的一个指定的集合做 left outer join(左外连接)。对该阶段的每一个输入记录,
    // lookup 会在该记录中增加一个数组字段,该数组是被联表中满足匹配条件的记录列表。lookup 会将连接后的结果输出给下个阶段。

    // 左连接:以左边表为基表,左表有的右表也有就也出来,右表没有的就NULL,右连接和左连接相反
    // 内连接:两个表的交集,就是左表和右表都有的才显示出来
    // 全连接:连个表的并集(UNION:并集不重复,UNION ALL:并集重复)
    // 交叉连接:有两种,显式的和隐式的,不带ON子句,返回的是两表的乘积,也叫笛卡尔积.
    // 左外连接:以左表为主表,右表没数据为null
    // 右外连接:以右表为主表,左表没数据为null
    // 全外连接:全外 = 左外 UNION 右外
    'use strict';
    const db = uniCloud.database();
    const $ = db.command.aggregate;
    exports.main = async(event, context) => {
        let res = await db.collection('orders').aggregate()
            // 通过一个相等匹配条件连接 orders 和 books 集合,匹配的字段是 orders 集合的 book 字段和 books 集合的 title 字段
            // where orders.book = books.title
            .lookup({
                from: 'books',
                localField: 'book',
                foreignField: 'title',
                as: 'bookList',
            })
            .end()
        return res;
    };

    // 聚合之后的返回值
    // 应用:将A表中用户在B表中的数据提取出来,插入到A表输出
    // {
    //  "affectedDocs": 3,
    //  "data": [{
    //          "_id": 4,
    //          "book": "novel 1",
    //          "price": 30,
    //          "quantity": 2,
    //          // 左表全输出,并插入符合查询条件 where orders.book = books.title 查询集合
    //          "bookList": [{
    //              "_id": "book1",
    //              "author": "author 1",
    //              "category": "novel",
    //              "stock": 10,
    //              "time": 1564456048486,
    //              "title": "novel 1"
    //          }]
    //      },
    //      {
    //          "_id": 5,
    //          "book": "science 1",
    //          "price": 20,
    //          "quantity": 1,
    //          // 左表全输出,并插入符合查询条件 where orders.book = books.title 查询集合
    //          "bookList": [{
    //              "_id": "book3",
    //              "author": "author 3",
    //              "category": "science",
    //              "stock": 30,
    //              "title": "science 1"
    //          }]
    //      },
    //      {
    //          "_id": 6,
    //          // 左表全输出,并插入符合查询条件 where orders.book = books.title 查询集合
    //          // 两条数据都没有title
    //          "bookList": [{
    //                  "_id": "book5",
    //                  "author": "author 4",
    //                  "category": "science",
    //                  "stock": 50
    //              },
    //              {
    //                  "_id": "book6",
    //                  "author": "author 5",
    //                  "category": "novel",
    //                  "stock": "60"
    //              }
    //          ]
    //      }
    //  ]
    // }
  • 相关阅读:
    DataFrame转矩阵Np-Array
    十月15
    十月14
    十月14
    十月12
    十月10
    2016-02-22 有无网络 2
    2016-02-22 有无网络的提示 1 h m
    2016-02-20 web view
    20160220 下拉刷新 上拉加载
  • 原文地址:https://www.cnblogs.com/luwei0915/p/13390088.html
Copyright © 2011-2022 走看看