Docs 菜单
Docs 主页
/ / /
Node.js 驱动程序
/

多字段连接

在此页面上

  • 简介
  • 聚合任务摘要
  • 开始之前
  • Tutorial
  • 添加查找阶段以链接collection和导入字段
  • 为 2020 年订购的产品添加匹配阶段
  • 添加未设置阶段以删除不需要的字段
  • 运行聚合管道
  • 解释结果

在本教程中,您可以学习如何使用 Node.js 驱动程序构建聚合管道,对collection执行聚合,并通过完成和运行示例应用来打印结果。

此聚合执行多字段联接。 当您用来将文档匹配在一起的两个collection的文档中有多个相应字段时,就会发生多字段连接。聚合将这些文档的字段值进行匹配,并将两者的信息合并到一个文档中。

提示

一对多连接

一对多联接是多字段联接的一种变体。 执行一对多联接时,您可以从一个文档中选择一个字段,该字段与联接另一端的多个文档中的字段值匹配。 要了解有关这些数据关系的详情,请参阅维基百科有关 一对多(数据模型) 的条目 多对多(数据模型)。

本教程演示如何将描述产品信息的集合与描述客户订单的另一个集合中的数据组合起来。 结果显示 2020 年订购的产品列表,其中还包含每个订单的详细信息。

此示例使用两个集合:

  • products,其中包含描述商店销售的产品的文档

  • orders,其中包含描述商店中产品的单个订单的文档

一份订单只能包含一种产品,因此聚合使用多字段联接将产品文档与表示该产品订单的文档进行匹配。 这些collection由productscollection中的文档中的namevariation字段连接,对应于orderscollection中的文档中的product_nameproduct_variation字段。

在开始本教程之前,请完成聚合模板应用说明,设置有效的 Node.js 应用程序。

设置应用后,通过将以下代码添加到应用程序来访问productsorderscollection:

const productsColl = aggDB.collection("products");
const ordersColl = aggDB.collection("orders");

删除所有现有数据,并将样本数据插入products collection,如以下代码所示:

await productsColl.deleteMany({});
const productsData = [
{
name: "Asus Laptop",
variation: "Ultra HD",
category: "ELECTRONICS",
description: "Great for watching movies",
},
{
name: "Asus Laptop",
variation: "Standard Display",
category: "ELECTRONICS",
description: "Good value laptop for students",
},
{
name: "The Day Of The Triffids",
variation: "1st Edition",
category: "BOOKS",
description: "Classic post-apocalyptic novel",
},
{
name: "The Day Of The Triffids",
variation: "2nd Edition",
category: "BOOKS",
description: "Classic post-apocalyptic novel",
},
{
name: "Morphy Richards Food Mixer",
variation: "Deluxe",
category: "KITCHENWARE",
description: "Luxury mixer turning good cakes into great",
},
];
await productsColl.insertMany(productsData);

删除所有现有数据,并将样本数据插入orders collection,如以下代码所示:

await ordersColl.deleteMany({});
const orderData = [
{
customer_id: "elise_smith@myemail.com",
orderdate: new Date("2020-05-30T08:35:52Z"),
product_name: "Asus Laptop",
product_variation: "Standard Display",
value: 431.43,
},
{
customer_id: "tj@wheresmyemail.com",
orderdate: new Date("2019-05-28T19:13:32Z"),
product_name: "The Day Of The Triffids",
product_variation: "2nd Edition",
value: 5.01,
},
{
customer_id: "oranieri@warmmail.com",
orderdate: new Date("2020-01-01T08:25:37Z"),
product_name: "Morphy Richards Food Mixer",
product_variation: "Deluxe",
value: 63.13,
},
{
customer_id: "jjones@tepidmail.com",
orderdate: new Date("2020-12-26T08:55:46Z"),
product_name: "Asus Laptop",
product_variation: "Standard Display",
value: 429.65,
},
];
await ordersColl.insertMany(orderData);
1

管道的第一个阶段是$lookup阶段,用于通过每个集合中的两个字段将orders集合连接到products集合。 查找阶段包含一个用于配置联接的嵌入式管道。

在嵌入式管道中,添加$match阶段以匹配联接两侧的两个字段的值。 请注意,以下代码使用创建 $lookup 阶段时设置的namevariation字段的别名:

const embedded_pl = [];
embedded_pl.push({
$match: {
$expr: {
$and: [
{ $eq: ["$product_name", "$$prdname"] },
{ $eq: ["$product_variation", "$$prdvartn"] },
],
},
},
});

在嵌入式管道中,添加另一个$match阶段以匹配2020中的订单:

embedded_pl.push({
$match: {
orderdate: {
$gte: new Date("2020-01-01T00:00:00Z"),
$lt: new Date("2021-01-01T00:00:00Z"),
},
},
});

在嵌入式管道中,添加$unset阶段以从联接的orders集合端删除不需要的字段:

embedded_pl.push({
$unset: ["_id", "product_name", "product_variation"],
});

嵌入式管道完成后,将$lookup阶段添加到主聚合管道。 配置此阶段以将处理后的查找字段存储在名为orders的数组字段中:

pipeline.push({
$lookup: {
from: "orders",
let: {
prdname: "$name",
prdvartn: "$variation",
},
pipeline: embedded_pl,
as: "orders",
},
});
2

接下来,添加一个$match阶段,以仅显示基于上一步中计算的orders数组在2020中至少有一个订单的产品:

pipeline.push({
$match: {
orders: { $ne: [] },
},
});
3

最后,添加一个$unset阶段。 $unset阶段从结果文档中删除_iddescription字段:

pipeline.push({
$unset: ["_id", "description"],
});
4

将以下代码添加到应用程序末尾,以对productscollection执行聚合:

const aggregationResult = await productsColl.aggregate(pipeline);

最后,在 shell 中运行以下命令以启动应用程序:

node agg_tutorial.js
5

聚合结果包含两个文档。 这些文档代表 2020 年有订单的产品。 每个文档都包含一个orders数组字段,其中列出了该产品的每个订单的详细信息:

{
name: 'Asus Laptop',
variation: 'Standard Display',
category: 'ELECTRONICS',
orders: [
{
customer_id: 'elise_smith@myemail.com',
orderdate: 2020-05-30T08:35:52.000Z,
value: 431.43
},
{
customer_id: 'jjones@tepidmail.com',
orderdate: 2020-12-26T08:55:46.000Z,
value: 429.65
}
]
}
{
name: 'Morphy Richards Food Mixer',
variation: 'Deluxe',
category: 'KITCHENWARE',
orders: [
{
customer_id: 'oranieri@warmmail.com',
orderdate: 2020-01-01T08:25:37.000Z,
value: 63.13
}
]
}

结果文档包含orders集合和products集合中文档的详细信息,并由产品名称和变体连接起来。

要查看本教程的完整代码,请参阅 Completed Multi-field Join App 在Github 上。

后退

一对一连接