码迷,mamicode.com
首页 > 数据库 > 详细

demo_10_06 云数据库聚合_lookup_01

时间:2020-07-28 13:56:39      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:science   reg   笛卡尔积   union   wait   outer   cte   提取   end   

// 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"
//              }
//          ]
//      }
//  ]
// }

demo_10_06 云数据库聚合_lookup_01

标签:science   reg   笛卡尔积   union   wait   outer   cte   提取   end   

原文地址:https://www.cnblogs.com/luwei0915/p/13390088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!