Docs 菜单
Docs 主页
/ / /
pymongo
/ /

一对一连接

在此页面上

  • 简介
  • 聚合任务摘要
  • 开始之前
  • 教程
  • 为 2020 年订单添加匹配阶段
  • 添加查找阶段以链接collection
  • 添加设置阶段以创建新文档字段
  • 添加未设置阶段以删除不需要的字段
  • 运行聚合管道
  • 解释结果

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

此聚合执行一对一联接。 当一个collection中的文档的字段值与另一个collection中具有相同字段值的单个文档相匹配时,就会发生一对一连接。聚合根据字段值匹配这些文档,并将两个来源的信息组合成一个结果。

提示

一对一联接不要求文档具有一对一的关系。 要了解有关此数据关系的更多信息,请参阅有关 一对一(数据模型)的 Wikipedia 条目。

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

此示例使用两个集合:

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

  • products:包含描述商店销售的产品的文档

一份订单只能包含一种产品,因此聚合使用一对一联接将订单文档与产品文档进行匹配。 这两个collection通过一个名为product_id的字段连接,该字段存在于两个collection的文档中。

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

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

orders_coll = agg_db["orders"]
products_coll = agg_db["products"]

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

orders_coll.delete_many({})
order_data = [
{
"customer_id": "elise_smith@myemail.com",
"orderdate": datetime(2020, 5, 30, 8, 35, 52),
"product_id": "a1b2c3d4",
"value": 431.43
},
{
"customer_id": "tj@wheresmyemail.com",
"orderdate": datetime(2019, 5, 28, 19, 13, 32),
"product_id": "z9y8x7w6",
"value": 5.01
},
{
"customer_id": "oranieri@warmmail.com",
"orderdate": datetime(2020, 1, 1, 8, 25, 37),
"product_id": "ff11gg22hh33",
"value": 63.13
},
{
"customer_id": "jjones@tepidmail.com",
"orderdate": datetime(2020, 12, 26, 8, 55, 46),
"product_id": "a1b2c3d4",
"value": 429.65
}
]
orders_coll.insert_many(order_data)

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

products_coll.delete_many({})
product_data = [
{
"id": "a1b2c3d4",
"name": "Asus Laptop",
"category": "ELECTRONICS",
"description": "Good value laptop for students"
},
{
"id": "z9y8x7w6",
"name": "The Day Of The Triffids",
"category": "BOOKS",
"description": "Classic post-apocalyptic novel"
},
{
"id": "ff11gg22hh33",
"name": "Morphy Richardds Food Mixer",
"category": "KITCHENWARE",
"description": "Luxury mixer turning good cakes into great"
},
{
"id": "pqr678st",
"name": "Karcher Hose Set",
"category": "GARDEN",
"description": "Hose + nosels + winder for tidy storage"
}
]
products_coll.insert_many(product_data)
1

添加$match阶段,用于匹配2020中所下订单:

pipeline.append({
"$match": {
"orderdate": {
"$gte": datetime(2020, 1, 1, 0, 0, 0),
"$lt": datetime(2021, 1, 1, 0, 0, 0)
}
}
})
2

接下来,添加一个$lookup阶段。 阶段将 集合中的 字段连接到$lookupproduct_id ordersidproducts集合中的 字段:

pipeline.append({
"$lookup": {
"from": "products",
"localField": "product_id",
"foreignField": "id",
"as": "product_mapping"
}
})
3

接下来,向管道添加两个$set阶段。

第一个$set阶段将product_mapping字段设置为上一个$lookup阶段中创建的product_mapping对象中的第一个元素。

第二个$set阶段根据product_mapping对象字段中的值创建两个新字段product_nameproduct_category

pipeline.extend([
{
"$set": {
"product_mapping": {"$first": "$product_mapping"}
}
},
{
"$set": {
"product_name": "$product_mapping.name",
"product_category": "$product_mapping.category"
}
}
])

提示

由于这是一对一连接,因此$lookup阶段仅向输入文档添加一个数组元素。 管道使用$first操作符检索此元素中的数据。

4

最后,添加一个$unset阶段。 $unset阶段从文档中删除不必要的字段:

pipeline.append({"$unset": ["_id", "product_id", "product_mapping"]})
5

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

aggregation_result = orders_coll.aggregate(pipeline)

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

python3 agg_tutorial.py
6

聚合结果包含三个文档。 这些文档表示 2020 年发生的客户订单,以及所订购产品的product_nameproduct_category

{
'customer_id': 'elise_smith@myemail.com',
'orderdate': datetime.datetime(2020, 5, 30, 8, 35, 52),
'value': 431.43,
'product_name': 'Asus Laptop',
'product_category': 'ELECTRONICS'
}
{
'customer_id': 'oranieri@warmmail.com',
'orderdate': datetime.datetime(2020, 1, 1, 8, 25, 37),
'value': 63.13,
'product_name': 'Morphy Richardds Food Mixer',
'product_category': 'KITCHENWARE'
}
{
'customer_id': 'jjones@tepidmail.com',
'orderdate': datetime.datetime(2020, 12, 26, 8, 55, 46),
'value': 429.65,
'product_name': 'Asus Laptop',
'product_category': 'ELECTRONICS'
}

结果由包含来自orders集合和products集合中的文档字段的文档组成,通过匹配每个原始文档中存在的product_id字段来连接这些集合。

要查看本教程的完整代码,请参阅已 完成的一对一加入应用 在Github 上。

后退

解压数组并进行分组

来年

多字段连接