详解MongoDB中的多表关联查询($lookup)
<h2>一. 聚合框架 </h2><p>聚合框架是MongoDB的高级查询语言,它允许我们通过转换和合并多个文档中的数据来生成新的单个文档中不存在的信息。</p>
<p>聚合管道操作主要包含下面几个部分:</p>
<table style="height: 287px; width: 324px" border="0">
<tbody>
<tr>
<td style="text-align: center"><strong><span style="font-size: 15px">命令</span></strong></td>
<td style="text-align: center"><strong><span style="font-size: 15px">功能描述</span></strong></td>
</tr>
<tr>
<td style="text-align: left"><span style="font-size: 14px">$project</span></td>
<td style="text-align: left"><span style="font-size: 14px">指定输出文档里的字段.</span></td>
</tr>
<tr>
<td style="text-align: left"><span style="font-size: 14px">$match</span></td>
<td style="text-align: left"><span style="font-size: 14px">选择要处理的文档,与fine()类似。</span></td>
</tr>
<tr>
<td style="text-align: left"><span style="font-size: 14px">$limit</span></td>
<td style="text-align: left"><span style="font-size: 14px">限制传递给下一步的文档数量。</span></td>
</tr>
<tr>
<td style="text-align: left"><span style="font-size: 14px">$skip</span></td>
<td style="text-align: left"><span style="font-size: 14px">跳过一定数量的文档。</span></td>
</tr>
<tr>
<td style="text-align: left"><span style="font-size: 14px">$unwind</span></td>
<td style="text-align: left"><span style="font-size: 14px">扩展数组,为每个数组入口生成一个输出文档。</span></td>
</tr>
<tr>
<td style="text-align: left"><span style="font-size: 14px">$group</span></td>
<td style="text-align: left"><span style="font-size: 14px">根据key来分组文档。</span></td>
</tr>
<tr>
<td style="text-align: left"><span style="font-size: 14px">$sort</span></td>
<td style="text-align: left"><span style="font-size: 14px">排序文档。</span></td>
</tr>
<tr>
<td style="text-align: left"><span style="font-size: 14px">$geoNear</span></td>
<td style="text-align: left"><span style="font-size: 14px">选择某个地理位置附近的的文档。</span></td>
</tr>
<tr>
<td style="text-align: left"><span style="font-size: 14px">$out</span></td>
<td style="text-align: left"><span style="font-size: 14px">把管道的结果写入某个集合。</span></td>
</tr>
<tr>
<td style="text-align: left"><span style="font-size: 14px">$redact</span></td>
<td style="text-align: left"><span style="font-size: 14px">控制特定数据的访问。</span></td>
</tr>
<tr>
<td style="text-align: left">
<p><em><span style="background-color: rgba(255, 255, 255, 1); color: rgba(128, 0, 0, 1)"><strong><span style="font-size: 14px">$lookup</span></strong></span></em></p>
</td>
<td style="text-align: left"><em><span style="background-color: rgba(255, 255, 255, 1); color: rgba(128, 0, 0, 1)"><strong><span style="font-size: 14px">多表关联(3.2版本新增)</span></strong></span></em></address></td>
</tr>
</tbody>
</table>
<p>在本篇幅中,我们聚焦$lookup的使用。</p>
<h2>二. $lookup的功能及语法</h2>
<p><span style="font-size: 15px"><strong>1. 主要功能 </strong></span>是将每个输入待处理的文档,经过$lookup 阶段的处理,输出的新文档中会包含一个新生成的数组列(户名可根据需要命名新key的名字 )。数组列存放的数据 是 来自 被Join 集合的适配文档,如果没有,集合为空(即 为[ ])</p>
<p><span style="font-size: 15px"><strong>2.</strong> <strong>基本语法</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
$lookup:
{
</span><span style="color: rgba(0, 0, 255, 1)">from</span>: <span style="color: rgba(128, 128, 128, 1)"><</span>collection <span style="color: rgba(0, 0, 255, 1)">to</span> <span style="color: rgba(128, 128, 128, 1)">join></span><span style="color: rgba(0, 0, 0, 1)">,
localField: </span><span style="color: rgba(128, 128, 128, 1)"><</span>field <span style="color: rgba(0, 0, 255, 1)">from</span> the input documents<span style="color: rgba(128, 128, 128, 1)">></span><span style="color: rgba(0, 0, 0, 1)">,
foreignField: </span><span style="color: rgba(128, 128, 128, 1)"><</span>field <span style="color: rgba(0, 0, 255, 1)">from</span> the documents <span style="color: rgba(0, 0, 255, 1)">of</span> the "<span style="color: rgba(0, 0, 255, 1)">from</span>" collection<span style="color: rgba(128, 128, 128, 1)">></span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 0, 255, 1)">as</span>: <span style="color: rgba(128, 128, 128, 1)"><</span>output array field<span style="color: rgba(128, 128, 128, 1)">></span><span style="color: rgba(0, 0, 0, 1)">
}
}</span></pre>
</div>
<p> <span style="font-size: 15px"><strong>3. 语法的解释说明</strong></span></p>
<table border="0">
<tbody>
<tr>
<td style="text-align: center"><span style="font-size: 15px"><strong>语法值</strong></span></td>
<td style="text-align: center"><span style="font-size: 15px"><strong>解释说明</strong></span></td>
</tr>
<tr>
<td style="text-align: left">
<pre><span style="font-size: 14px">from</span></pre>
</td>
<td><span style="font-size: 14px">同一个数据库下等待被Join的集合。</span></td>
</tr>
<tr>
<td>
<pre><span style="font-size: 14px">localField</span></pre>
</td>
<td>
<p><span style="font-size: 14px">源集合中的match值,如果输入的集合中,某文档没有 localField</span></p>
<p><span style="font-size: 14px">这个Key(Field),在处理的过程中,会默认为此文档含</span></p>
<p><span style="font-size: 14px">有 localField:null的键值对。</span></p>
</td>
</tr>
<tr>
<td>
<pre><span style="font-size: 14px">foreignField</span></pre>
</td>
<td><span style="font-size: 15px">待Join的集合的match值,如果待Join的集合中,文档没有foreignField</span><br><span style="font-size: 15px">值,在处理的过程中,会默认为此文档含有 foreignField:null的键值对。</span></td>
</tr>
<tr>
<td>
<pre><span style="font-size: 14px">as</span></pre>
</td>
<td><span style="font-size: 14px">为输出文档的新增值命名。如果输入的集合中已存在该值,则会覆盖掉,</span></td>
</tr>
</tbody>
</table>
<p> </p>
<p>(注:null = null 此为真)</p>
<p>其语法功能类似于下面的伪SQL语句:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 128, 128, 1)">*</span>, <span style="color: rgba(128, 128, 128, 1)"><</span>output array field<span style="color: rgba(128, 128, 128, 1)">></span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span><span style="color: rgba(0, 0, 0, 1)"> collection
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> <span style="color: rgba(128, 128, 128, 1)"><</span>output array field<span style="color: rgba(128, 128, 128, 1)">></span> <span style="color: rgba(128, 128, 128, 1)">IN</span> (<span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 128, 128, 1)">*</span>
<span style="color: rgba(0, 0, 255, 1)">FROM</span> <span style="color: rgba(128, 128, 128, 1)"><</span>collection <span style="color: rgba(0, 0, 255, 1)">to</span> <span style="color: rgba(128, 128, 128, 1)">join></span>
<span style="color: rgba(0, 0, 255, 1)">WHERE</span> <span style="color: rgba(128, 128, 128, 1)"><</span>foreignField<span style="color: rgba(128, 128, 128, 1)">>=</span> <span style="color: rgba(128, 128, 128, 1)"><</span>collection.localField<span style="color: rgba(128, 128, 128, 1)">></span>);</pre>
</div>
<p> </p>
<h2>三. 案例</h2>
<p>以上的语法介绍有些枯燥,不易理解,我们直接分析品味案例好了。</p>
<p>假设 有 <strong><span style="color: rgba(255, 0, 0, 1)">订单集合</span></strong>, 存储的测试数据 如下:</p>
<div class="cnblogs_code">
<pre>db.orders.<span style="color: rgba(0, 0, 255, 1)">insert</span>(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
{ "_id" : 3}
</span><span style="color: rgba(255, 0, 0, 1)">]</span>)</pre>
</div>
<p>其中 item 对应 数据为 <strong>商品名称</strong>。</p>
<p><span style="color: rgba(255, 0, 0, 1)"><span style="color: rgba(0, 0, 0, 1)">另外 一个 就是就是</span><strong> 商品库存集合</strong></span> ,存储的测试数据 如下:</p>
<div class="cnblogs_code">
<pre>db.inventory.<span style="color: rgba(0, 0, 255, 1)">insert</span>(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{ "_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 }
</span><span style="color: rgba(255, 0, 0, 1)">]</span>)</pre>
</div>
<p>此集合中的 sku 数据等同于 订单 集合中的 <strong>商品名称</strong>。</p>
<p>在这种模式设计下,如果要查询订单表对应商品的库存情况,应如何写代码呢?</p>
<p><strong>很明显这需要两个集合Join。</strong></p>
<p>场景简单,不做赘述,直送答案 。其语句 如下:</p>
<div class="cnblogs_code">
<pre>db.orders.aggregate(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span>)</pre>
</div>
<p>返回的执行结果如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">"),
"item" : "almonds",
"price" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">12</span><span style="color: rgba(0, 0, 0, 1)">"),
"quantity" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">"),
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
"_id" : NumberInt("1"),
"sku" : "almonds",
"description" : "product 1",
"instock" : NumberInt("120")
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}
{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">"),
"item" : "pecans",
"price" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">20</span><span style="color: rgba(0, 0, 0, 1)">"),
"quantity" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">"),
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
"_id" : NumberInt("4"),
"sku" : "pecans",
"description" : "product 4",
"instock" : NumberInt("70")
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}
{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)">"),
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
"_id" : NumberInt("5"),
"sku" : null,
"description" : "Incomplete"
},
{
"_id" : NumberInt("6")
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p> </p>
<p>分析查询语句和结果,回扣$lookup定义,可以将上面的处理过程,描述如下:</p>
<p style="text-align: left">从集合order中逐个获取文档处理,拿到一个文档后,会根据localField 值 遍历 被 Join的 inventory集合(from: "inventory"),看inventory集合文档中 foreignField值是否与之相等。如果相等,就把符合条件的inventory文档 整体 内嵌到聚合框架新生成的文档中,并且新key 统一命名为 inventory_docs。考虑到符合条件的文档不唯一,这个Key对应的Value是个数组形式。原集合中Key对应的值为Null值或不存在时,需特别小心。</p>
<h2>四. 说明</h2>
<p>在以下的说明中,为描述方便,将 from对应的集合定义为 <strong>被join集合;</strong>待聚合的表成为<strong>源表; </strong>将 localField 和 foreignField 对应的Key 定义 <strong>比较列。</strong></p>
<p><span style="font-size: 15px"><strong><span style="background-color: rgba(136, 136, 136, 1)">1.</span> </strong></span>因客户端工具显示的问题,上面示例中查询结果重Int 类型值都自动显示为了 NumberInt("")。这个NumberInt标注,请忽略,不影响我们的功能测试。</p>
<p><strong><span style="font-size: 15px; background-color: rgba(136, 136, 136, 1)">2.</span> </strong>这个示例中,一共输出了三个文档,在没有再次聚合($match)的条件下,这个输出文档数量是以输入文档的数量来决定的(由order来决定),而不是以被Join的集合(<span class="s2">inventory</span>)文档数量决定。</p>
<p><strong><span style="font-size: 15px; background-color: rgba(136, 136, 136, 1)">3.</span> </strong>在此需要特别强调的是输出的第三个文档。在源库中原文档没有要比较的列(即item值不存在,既不是Null值,也不是值为空),此时 和 被Join 集合比较,如果 被Join集合中 比较列 也恰好 为NUll 或 不存在的值,此时,判断相等 ,即会把 被Join集合中 比较列 为NUll 或 值不存在 文档 吸收进来。</p>
<p><span style="font-size: 15px; background-color: rgba(136, 136, 136, 1)"><strong>4. </strong></span>假设 源表(order) 中比较列 为某一个值,而此值在待比较表(inventory)的所有文档中都不存在,那么查询结果会是什么样子呢?</p>
<p>order 集合在现有数据的基础上,再被insert 进一笔测试数据,这个订单的商品为 Start,在库存商品中根本没有此数据。</p>
<div class="cnblogs_code">
<pre>db.orders.<span style="color: rgba(0, 0, 255, 1)">insert</span>({"_id" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">4</span>, "item" : "Start", "price" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2000</span>, "quantity" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span> })</pre>
</div>
<p>order集合的文档数量由之前的3个增长为4个。</p>
<p>再次执行查询</p>
<div class="cnblogs_code">
<pre>db.orders.aggregate(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span>)</pre>
</div>
<p>此时查看结果 </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">"),
"item" : "almonds",
"price" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">12</span><span style="color: rgba(0, 0, 0, 1)">"),
"quantity" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">"),
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
"_id" : NumberInt("1"),
"sku" : "almonds",
"description" : "product 1",
"instock" : NumberInt("120")
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}
{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">"),
"item" : "pecans",
"price" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">20</span><span style="color: rgba(0, 0, 0, 1)">"),
"quantity" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">"),
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
"_id" : NumberInt("4"),
"sku" : "pecans",
"description" : "product 4",
"instock" : NumberInt("70")
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}
{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span><span style="color: rgba(0, 0, 0, 1)">"),
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
"_id" : NumberInt("5"),
"sku" : null,
"description" : "Incomplete"
},
{
"_id" : NumberInt("6")
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}
{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">4</span><span style="color: rgba(0, 0, 0, 1)">"),
"item" : "Start",
"price" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2000</span><span style="color: rgba(0, 0, 0, 1)">"),
"quantity" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">"),
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span> <span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p> 查询出的结果也由之前的3个变成了4个。比较特别的是第四个文档 ,其<strong>新增列 为 "inventory_docs" : [ ] ,即值为空</strong> 。所以,此时,实现的功能非常像关系型数据库的 left join。</p>
<p>那么,可不可以<strong>只筛选出新增列为空的文档呢</strong>?</p>
<p>即 我们查询出 ,比较列的条件下,刷选出只在A集合中,而不在集合B的文档呢? 就像关系数据库中量表Join的<strong> left join on a.key =b.key where b.key is null</strong> .</p>
<p><strong>答案是可以的</strong>。</p>
<p>其实回到聚合框架上来,再次聚合一下就可以了,来一次$match就可以了。</p>
<p>执行的语句调整一下就OK了。</p>
<div class="cnblogs_code">
<pre>db.orders.aggregate(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
},
{ $match : {"inventory_docs" : [ </span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">} }
])</span></pre>
</div>
<p>执行结果 为 </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">4</span><span style="color: rgba(0, 0, 0, 1)">"),
"item" : "Start",
"price" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2000</span><span style="color: rgba(0, 0, 0, 1)">"),
"quantity" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">"),
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span> <span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>可以看出执行结果只有一个文档。这个文档表明的含义是:订单中有这个商品,但是库存中没有这个商品。</p>
<p>(<strong>$look只是聚合框架的一个stage,在其前前后后,都可以嵌入到其他的聚合管道的命令,例如$match.$group等。</strong>下面的说明5,也可以说明一二)</p>
<p><span style="background-color: rgba(136, 136, 136, 1)"><strong><span style="font-size: 15px">5.</span> </strong></span>以上的比较列都是单一的Key/Value,如果复杂一点,如果<strong>比较的列是<span style="color: rgba(255, 0, 0, 1)">数组</span></strong>,我们又该如何关联呢?</p>
<p>我们接下来再来测试一把。将之前 集合order 、inventory 插入的数据清空。</p>
<p>插入此场景下的新数据,向order中插入的数据,如下:</p>
<div class="cnblogs_code">
<pre>db.orders.<span style="color: rgba(0, 0, 255, 1)">insert</span>({ "_id" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, "item" : "MON1003", "price" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">350</span>, "quantity" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span>, "specs" :<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)"> "27 inch", "Retina display", "1920x1080" </span><span style="color: rgba(255, 0, 0, 1)">]</span>, "type" : "Monitor" })</pre>
</div>
<pre>specs 对应的value是数组格式。</pre>
<p>向集合inventory 新插入的数据 如下:</p>
<div class="cnblogs_code">
<pre>db.inventory.<span style="color: rgba(0, 0, 255, 1)">insert</span>({ "_id" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, "sku" : "MON1003", "type" : "Monitor", "instock" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">120</span>,"size" : "<span style="color: rgba(128, 0, 0, 1); font-weight: bold">27</span> inch", "resolution" : "192<span style="color: rgba(128, 0, 0, 1); font-weight: bold">0x1080</span><span style="color: rgba(0, 0, 0, 1)">" })
db.inventory.</span><span style="color: rgba(0, 0, 255, 1)">insert</span>({ "_id" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span>, "sku" : "MON1012", "type" : "Monitor", "instock" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">85</span>,"size" : "<span style="color: rgba(128, 0, 0, 1); font-weight: bold">23</span> inch", "resolution" : "128<span style="color: rgba(128, 0, 0, 1); font-weight: bold">0x800</span><span style="color: rgba(0, 0, 0, 1)">" })
db.inventory.</span><span style="color: rgba(0, 0, 255, 1)">insert</span>({ "_id" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">3</span>, "sku" : "MON1031", "type" : "Monitor", "instock" : <span style="color: rgba(128, 0, 0, 1); font-weight: bold">60</span>,"size" : "<span style="color: rgba(128, 0, 0, 1); font-weight: bold">23</span> inch", "display_type" : "LED" })</pre>
</div>
<p>查询的语句如下:</p>
<div class="cnblogs_code">
<pre>db.orders.aggregate(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
$unwind: "$specs"
},
{
$lookup:
{
from: "inventory",
localField: "specs",
foreignField: "size",
as: "inventory_docs"
}
},
{
$match: { "inventory_docs": { $ne: [</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)"> } }
}
])</span></pre>
</div>
<p>查询显示结果如下:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">"),
"item" : "MON1003",
"price" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">350</span><span style="color: rgba(0, 0, 0, 1)">"),
"quantity" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">"),
"specs" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">27</span><span style="color: rgba(0, 0, 0, 1)"> inch",
"type" : "Monitor",
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
"_id" : NumberInt("1"),
"sku" : "MON1003",
"type" : "Monitor",
"instock" : NumberInt("120"),
"size" : "27 inch",
"resolution" : "1920x1080"
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>仔细看啊,输出文档中的 specs 对应的数据变成了字符串类型(原集合为数组)。是什么发挥了如此神奇功效???,请看黑板,请将目光集中在</p>
<pre>{
$unwind: "$specs"
}</pre>
<p>还有个小问题,大家猜一下,如果查询语句中没有</p>
<pre>{
$match: { "inventory_docs": { $ne: []<span> } }
}<br></span><br>结果会是什么样呢?即查看语句修改为:</pre>
<div class="cnblogs_code">
<pre>db.orders.aggregate(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
$unwind: "$specs"
},
{
$lookup:
{
from: "inventory",
localField: "specs",
foreignField: "size",
as: "inventory_docs"
}
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span>)</pre>
</div>
<p>大家猜猜看!</p>
<p>大家猜猜看!</p>
<p>大家猜猜看!</p>
<p>呵呵...此时的结果是:</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">文档1
{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">"),
"item" : "MON1003",
"price" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">350</span><span style="color: rgba(0, 0, 0, 1)">"),
"quantity" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">"),
"specs" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">27</span><span style="color: rgba(0, 0, 0, 1)"> inch",
"type" : "Monitor",
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{
"_id" : NumberInt("1"),
"sku" : "MON1003",
"type" : "Monitor",
"instock" : NumberInt("120"),
"size" : "27 inch",
"resolution" : "1920x1080"
}
</span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}
文档2
{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">"),
"item" : "MON1003",
"price" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">350</span><span style="color: rgba(0, 0, 0, 1)">"),
"quantity" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">"),
"specs" : "Retina display",
"type" : "Monitor",
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span> <span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}
文档3
{
"_id" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">"),
"item" : "MON1003",
"price" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">350</span><span style="color: rgba(0, 0, 0, 1)">"),
"quantity" : NumberInt("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">"),
"specs" : "192</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0x1080</span><span style="color: rgba(0, 0, 0, 1)">",
"type" : "Monitor",
"inventory_docs" : </span><span style="color: rgba(255, 0, 0, 1)">[</span> <span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>你推算出正确结果了吗?</p>
<p><span style="background-color: rgba(136, 136, 136, 1)">6. </span>最后一道题,在SQL中两表关联,每个表都有条件,那么在MongoDB中应该如何书写呢?</p>
<p>例如SQL Server 语句</p>
<div class="cnblogs_code">
<pre> <span style="color: rgba(0, 0, 255, 1)">SELECT</span> <span style="color: rgba(128, 128, 128, 1)">*</span> <span style="color: rgba(0, 0, 255, 1)">FROM</span> dbo.Rel_QQDetails <span style="color: rgba(0, 0, 255, 1)">AS</span><span style="color: rgba(0, 0, 0, 1)"> D
</span><span style="color: rgba(128, 128, 128, 1)">JOIN</span> dbo.Fct_QQStatements <span style="color: rgba(0, 0, 255, 1)">AS</span> S <span style="color: rgba(0, 0, 255, 1)">ON</span> D.OrderId<span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)">s.OrderStatementsId
</span><span style="color: rgba(0, 0, 255, 1)">WHERE</span> D.ReconciliationId<span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(255, 0, 0, 1)">'</span><span style="color: rgba(255, 0, 0, 1)">te54test7-187e-4e38-85e7-88926000aa7a</span><span style="color: rgba(255, 0, 0, 1)">'</span>
<span style="color: rgba(128, 128, 128, 1)">AND</span> S.StatementsPriceException<span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(255, 0, 0, 1)">'false</span><span style="color: rgba(255, 0, 0, 1)">'</span></pre>
</div>
<p>转换后的MongoDB代码为:</p>
<div class="cnblogs_code">
<pre>db.Rel_QQDetails.aggregate(<span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
{ $match: {ReconciliationId:CSUUID("bb54bee7-187f-4d38-85d7-88926000ac7a")}},
{ $lookup:
{
from: "Fct_QQStatements",
localField: "OrderId",
foreignField: "OrderStatementsId",
as: "inventory_docs"
}
},
{ $match : {"inventory_docs.StatementsPriceException" :false} }
</span><span style="color: rgba(255, 0, 0, 1)">]</span>)</pre>
</div>
<p> </p>
<p><span style="color: rgba(0, 0, 0, 1); background-color: rgba(128, 128, 128, 1)">7.</span> 附加题,mongodb 集合间关联后更新,在MongoDB中应该如何书写呢?----借助 forEach 功能</p>
<p>由于篇幅限制,集合中的数据格式不再说明。</p>
<p>需求:集合QQ_OrderReturn 和 RelQQ_ReconciliationDetails 关联刷选,刷选符合条件,在更新QQ_OrderReturn的数据。</p>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">db.QQ_OrderReturn.aggregate([
{$match:{</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Status</span><span style="color: rgba(128, 0, 0, 1)">"</span> : <span style="color: rgba(128, 0, 128, 1)">21</span><span style="color: rgba(0, 0, 0, 1)">}},
{$match:{</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Disabled</span><span style="color: rgba(128, 0, 0, 1)">"</span> : <span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">}},
{$match:{</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">JoinResponParty</span><span style="color: rgba(128, 0, 0, 1)">"</span> : <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">合作方</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">}},
{$match:{ SupplierSellerName:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">(合作营)ABC阳澄湖蟹</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">}},
{
$lookup:
{
from: </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">RelQQ_ReconciliationDetails</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
localField: </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">OrderReturnId</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
foreignField: </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">OrderId</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">,
as: </span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">inventory_docs</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
}
},
{ $match : {</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">inventory_docs</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> : [ ]} }
]).forEach(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(item){
db.QQ_OrderReturn.update({</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">_id</span><span style="color: rgba(128, 0, 0, 1)">"</span>:item._id},{$set:{<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Status</span><span style="color: rgba(128, 0, 0, 1)">"</span>:NumberInt(<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">)}})
})</span></pre>
</div>
<p> </p>
<p> </p>
<p>谢谢!!!</p>
<p>希望以上的讲解和演示能对大家学习$lookup有所帮助。</p>
<p> </p>
<p> </p>
<p><strong>注:以上案例数据参考MongoDB官方网站,大家也可访问官网获取更多、更全的相关知识。</strong></p>
<p> </p>
<p><strong>本文版权归作者所有,未经作者同意不得转载,谢谢配合!!!</strong></p>
<pre><em id="__mceDel"><br><br></em></pre><br><br>
来源:https://www.cnblogs.com/xuliuzai/p/10055535.html
頁:
[1]