对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

执行多字段联接

本教程说明了如何构建聚合管道、对集合执行聚合以及使用您选择的语言显示结果。

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

此聚合使用 $lookup 执行多字段连接。当两个集合的文档中有多个对应字段时,就会发生多字段联接。聚合在相应字段上匹配这些文档,并将两者的信息合并到一个文档中。


➤ 使用右上角的 Select your 语言)下拉菜单设立以下示例的语言,或选择MongoDB Shell。


此示例使用两个集合:

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

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

一份订单只能包含一种产品。该聚合使用多字段联接将产品文档与表示该产品订单的文档进行匹配。该聚合通过 products集合中的文档中的 name 和 variation 字段(对应于 orders集合中的文档中的 product_name 和 product_variation 字段)连接集合。

要创建 orders 和 products 集合,请使用 insertMany() 方法:

db.orders.deleteMany({}) db.orders.insertMany( [ { 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, }] )
db.products.deleteMany({}) db.products.insertMany( [ { 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" }] )

以下步骤演示如何创建和运行聚合管道以联接多个字段的集合。

1

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

embedded_pl = [ // Stage 1: Match the values of two fields on each side of the join // The $eq filter uses aliases for the name and variation fields set { $match: { $expr: { $and: [ { $eq: ["$product_name", "$$prdname"] }, { $eq: ["$product_variation", "$$prdvartn"] } ] } } }, // Stage 2: Match orders placed in 2020 { $match: { orderdate: { $gte: new Date("2020-01-01T00:00:00Z"), $lt: new Date("2021-01-01T00:00:00Z") } } }, // Stage 3: Remove unneeded fields from the orders collection side of the join { $unset: ["_id", "product_name", "product_variation"] }]
2
db.products.aggregate( [ // Use the embedded pipeline in a lookup stage { $lookup: { from: "orders", let: { prdname: "$name", prdvartn: "$variation" }, pipeline: embedded_pl, as: "orders" } }, // Match products ordered in 2020 { $match: { orders: { $ne: [] } } }, // Remove unneeded fields { $unset: ["_id", "description"] }] )
3

聚合结果包含两个文档。这些文档表示订购的产品 2020。每个文档都包含一个 orders大量字段,其中列出了该产品的每个订单的详细信息。

{ name: 'Asus Laptop', variation: 'Standard Display', category: 'ELECTRONICS', orders: [ { customer_id: 'elise_smith@myemail.com', orderdate: ISODate('2020-05-30T08:35:52.000Z'), value: 431.43 }, { customer_id: 'jjones@tepidmail.com', orderdate: ISODate('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: ISODate('2020-01-01T08:25:37.000Z'), value: 63.13 } ]}
选择您的语言
给本页内容打分