侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

MongoDB中多表关联查询($lookup)的深入讲解

数据库  /  管理员 发布于 5年前   155

一.  聚合框架

聚合框架是MongoDB的高级查询语言,它允许我们通过转换和合并多个文档中的数据来生成新的单个文档中不存在的信息。

聚合管道操作主要包含下面几个部分:

命令 功能描述
$project 指定输出文档里的字段.
$match 选择要处理的文档,与fine()类似。
$limit 限制传递给下一步的文档数量。
$skip 跳过一定数量的文档。
$unwind 扩展数组,为每个数组入口生成一个输出文档。
$group 根据key来分组文档。
$sort 排序文档。
$geoNear 选择某个地理位置附近的的文档。
$out 把管道的结果写入某个集合。
$redact 控制特定数据的访问。

$lookup

多表关联(3.2版本新增)

在本篇幅中,我们聚焦$lookup的使用。

二.  $lookup的功能及语法

1. 主要功能 是将每个输入待处理的文档,经过$lookup 阶段的处理,输出的新文档中会包含一个新生成的数组列(户名可根据需要命名新key的名字 )。数组列存放的数据 是 来自 被Join 集合的适配文档,如果没有,集合为空(即 为[ ])

2. 基本语法

{ $lookup: { from: <collection to join>, localField: <field from the input documents>, foreignField: <field from the documents of the "from" collection>, as: <output array field> }}

3. 语法的解释说明

语法值 解释说明
from 同一个数据库下等待被Join的集合。
localField

源集合中的match值,如果输入的集合中,某文档没有 localField

这个Key(Field),在处理的过程中,会默认为此文档含

有 localField:null的键值对。

foreignField 待Join的集合的match值,如果待Join的集合中,文档没有foreignField
值,在处理的过程中,会默认为此文档含有 foreignField:null的键值对。
as 为输出文档的新增值命名。如果输入的集合中已存在该值,则会覆盖掉,

(注:null = null 此为真)

其语法功能类似于下面的伪SQL语句:

SELECT *, <output array field>FROM collectionWHERE <output array field> IN (SELECT *  FROM <collection to join>  WHERE <foreignField>= <collection.localField>);

三. 案例

以上的语法介绍有些枯燥,不易理解,我们直接分析品味案例好了。

假设 有 订单集合, 存储的测试数据 如下:

db.orders.insert([ { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 }, { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 }, { "_id" : 3 }])

其中 item 对应 数据为 商品名称。

另外 一个 就是就是 商品库存集合 ,存储的测试数据 如下:

db.inventory.insert([ { "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 }, { "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 }, { "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 }, { "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 }, { "_id" : 5, "sku": null, description: "Incomplete" }, { "_id" : 6 }])

此集合中的 sku 数据等同于 订单 集合中的 商品名称。

在这种模式设计下,如果要查询订单表对应商品的库存情况,应如何写代码呢?

很明显这需要两个集合Join。

场景简单,不做赘述,直送答案 。其语句 如下:

db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } }])

返回的执行结果如下:

{
    "_id" : NumberInt("1"),
    "item" : "almonds",
    "price" : NumberInt("12"),
    "quantity" : NumberInt("2"),
    "inventory_docs" : [
        {
            "_id" : NumberInt("1"),
            "sku" : "almonds",
            "description" : "product 1",
            "instock" : NumberInt("120")
        }
    ]
}


{
    "_id" : NumberInt("2"),
    "item" : "pecans",
    "price" : NumberInt("20"),
    "quantity" : NumberInt("1"),
    "inventory_docs" : [
        {
            "_id" : NumberInt("4"),
            "sku" : "pecans",
            "description" : "product 4",
            "instock" : NumberInt("70")
        }
    ]
}


{
    "_id" : NumberInt("3"),
    "inventory_docs" : [
        {
            "_id" : NumberInt("5"),
            "sku" : null,
            "description" : "Incomplete"
        },
        {
            "_id" : NumberInt("6")
        }
    ]
}

分析查询语句和结果,回扣$lookup定义,可以将上面的处理过程,描述如下:

从集合order中逐个获取文档处理,拿到一个文档后,会根据localField 值 遍历 被 Join的 inventory集合(from: "inventory"),看inventory集合文档中 foreignField值是否与之相等。如果相等,就把符合条件的inventory文档  整体 内嵌到聚合框架新生成的文档中,并且新key 统一命名为 inventory_docs。考虑到符合条件的文档不唯一,这个Key对应的Value是个数组形式。原集合中Key对应的值为Null值或不存在时,需特别小心。

四. 说明

在以下的说明中,为描述方便,将 from对应的集合定义为 被join集合;待聚合的表成为源表; 将 localField 和 foreignField 对应的Key 定义 比较列。

1. 因客户端工具显示的问题,上面示例中查询结果重Int 类型值都自动显示为了 NumberInt("")。这个NumberInt标注,请忽略,不影响我们的功能测试。

2. 这个示例中,一共输出了三个文档,在没有再次聚合($match)的条件下,这个输出文档数量是以输入文档的数量来决定的(由order来决定),而不是以被Join的集合(inventory)文档数量决定。

3. 在此需要特别强调的是输出的第三个文档。在源库中原文档没有要比较的列(即item值不存在,既不是Null值,也不是值为空),此时 和 被Join 集合比较,如果 被Join集合中 比较列 也恰好 为NUll 或 不存在的值,此时,判断相等 ,即会把 被Join集合中 比较列 为NUll 或 值不存在 文档 吸收进来。

4. 假设 源表(order) 中比较列 为某一个值,而此值在待比较表(inventory)的所有文档中都不存在,那么查询结果会是什么样子呢?

order 集合在现有数据的基础上,再被insert 进一笔测试数据,这个订单的商品为 Start,在库存商品中根本没有此数据。

db.orders.insert({"_id" : 4, "item" : "Start", "price" : 2000, "quantity" : 1 })

order集合的文档数量由之前的3个增长为4个。

再次执行查询

db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } }])

此时查看结果

{
    "_id" : NumberInt("1"),
    "item" : "almonds",
    "price" : NumberInt("12"),
    "quantity" : NumberInt("2"),
    "inventory_docs" : [
        {
            "_id" : NumberInt("1"),
            "sku" : "almonds",
            "description" : "product 1",
            "instock" : NumberInt("120")
        }
    ]
}

{
    "_id" : NumberInt("2"),
    "item" : "pecans",
    "price" : NumberInt("20"),
    "quantity" : NumberInt("1"),
    "inventory_docs" : [
        {
            "_id" : NumberInt("4"),
            "sku" : "pecans",
            "description" : "product 4",
            "instock" : NumberInt("70")
        }
    ]
}


{
    "_id" : NumberInt("3"),
    "inventory_docs" : [
        {
            "_id" : NumberInt("5"),
            "sku" : null,
            "description" : "Incomplete"
        },
        {
            "_id" : NumberInt("6")
        }
    ]
}


{
    "_id" : NumberInt("4"),
    "item" : "Start",
    "price" : NumberInt("2000"),
    "quantity" : NumberInt("1"),
    "inventory_docs" : [ ]
}

查询出的结果也由之前的3个变成了4个。比较特别的是第四个文档 ,其新增列 为 "inventory_docs" : [ ] ,即值为空 。所以,此时,实现的功能非常像关系型数据库的 left join。

那么,可不可以只筛选出新增列为空的文档呢?

即 我们查询出 ,比较列的条件下,刷选出只在A集合中,而不在集合B的文档呢? 就像关系数据库中量表Join的 left join on a.key =b.key where b.key is null .

答案是可以的。

其实回到聚合框架上来,再次聚合一下就可以了,来一次$match就可以了。

执行的语句调整一下就OK了。

db.orders.aggregate([ { $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } }, { $match : {"inventory_docs" : [ ]} }])

执行结果 为

{
    "_id" : NumberInt("4"),
    "item" : "Start",
    "price" : NumberInt("2000"),
    "quantity" : NumberInt("1"),
    "inventory_docs" : [ ]
}

可以看出执行结果只有一个文档。这个文档表明的含义是:订单中有这个商品,但是库存中没有这个商品。

($look只是聚合框架的一个stage,在其前前后后,都可以嵌入到其他的聚合管道的命令,例如$match.$group等。下面的说明5,也可以说明一二)

5. 以上的比较列都是单一的Key/Value,如果复杂一点,如果比较的列是数组,我们又该如何关联呢?

我们接下来再来测试一把。将之前 集合order 、inventory 插入的数据清空。

插入此场景下的新数据,向order中插入的数据,如下:

db.orders.insert({ "_id" : 1, "item" : "MON1003", "price" : 350, "quantity" : 2, "specs" :[ "27 inch", "Retina display", "1920x1080" ], "type" : "Monitor" })

specs 对应的value是数组格式。

向集合inventory 新插入的数据 如下:

db.inventory.insert({ "_id" : 1, "sku" : "MON1003", "type" : "Monitor", "instock" : 120,"size" : "27 inch", "resolution" : "1920x1080" })db.inventory.insert({ "_id" : 2, "sku" : "MON1012", "type" : "Monitor", "instock" : 85,"size" : "23 inch", "resolution" : "1280x800" })db.inventory.insert({ "_id" : 3, "sku" : "MON1031", "type" : "Monitor", "instock" : 60,"size" : "23 inch", "display_type" : "LED" })

查询的语句如下:

db.orders.aggregate([ { $unwind: "$specs" }, { $lookup:  {  from: "inventory",  localField: "specs",  foreignField: "size",  as: "inventory_docs" } }, { $match: { "inventory_docs": { $ne: [] } } }])

查询显示结果如下:

{
    "_id" : NumberInt("1"),
    "item" : "MON1003",
    "price" : NumberInt("350"),
    "quantity" : NumberInt("2"),
    "specs" : "27 inch",
    "type" : "Monitor",
    "inventory_docs" : [
        {
            "_id" : NumberInt("1"),
            "sku" : "MON1003",
            "type" : "Monitor",
            "instock" : NumberInt("120"),
            "size" : "27 inch",
            "resolution" : "1920x1080"
        }
    ]
}

仔细看啊,输出文档中的 specs 对应的数据变成了字符串类型(原集合为数组)。是什么发挥了如此神奇功效???,请看黑板,请将目光集中在

{ $unwind: "$specs" }

还有个小问题,大家猜一下,如果查询语句中没有

{ $match: { "inventory_docs": { $ne: [] } } }

结果会是什么样呢?即查看语句修改为:

db.orders.aggregate([ { $unwind: "$specs" }, { $lookup:  {  from: "inventory",  localField: "specs",  foreignField: "size",  as: "inventory_docs" } }])

大家猜猜看!

大家猜猜看!

大家猜猜看!

呵呵...此时的结果是:

文档1
{
    "_id" : NumberInt("1"),
    "item" : "MON1003",
    "price" : NumberInt("350"),
    "quantity" : NumberInt("2"),
    "specs" : "27 inch",
    "type" : "Monitor",
    "inventory_docs" : [
        {
            "_id" : NumberInt("1"),
            "sku" : "MON1003",
            "type" : "Monitor",
            "instock" : NumberInt("120"),
            "size" : "27 inch",
            "resolution" : "1920x1080"
        }
    ]
}

文档2
{
    "_id" : NumberInt("1"),
    "item" : "MON1003",
    "price" : NumberInt("350"),
    "quantity" : NumberInt("2"),
    "specs" : "Retina display",
    "type" : "Monitor",
    "inventory_docs" : [ ]
}

文档3

{
    "_id" : NumberInt("1"),
    "item" : "MON1003",
    "price" : NumberInt("350"),
    "quantity" : NumberInt("2"),
    "specs" : "1920x1080",
    "type" : "Monitor",
    "inventory_docs" : [ ]
}

你推算出正确结果了吗?

谢谢!!!

希望以上的讲解和演示能对大家学习$lookup有所帮助。

注:以上案例数据参考MongoDB官方网站,大家也可访问官网获取更多、更全的相关知识。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对AIDI的支持。


  • 上一条:
    MongoDB 3.6版本中bind_ip设置详解
    下一条:
    MongoDB中的常用语句总结大全
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客