-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmulti-field-join.py
161 lines (142 loc) · 4.08 KB
/
multi-field-join.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from pymongo import MongoClient
from datetime import datetime
uri = "<connection string>"
client = MongoClient(uri)
try:
agg_db = client["agg_tutorials_db"]
# start-colls
products_coll = agg_db["products"]
orders_coll = agg_db["orders"]
# end-colls
# start-insert-products
products_coll.delete_many({})
products_data = [
{
"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"
}
]
products_coll.insert_many(products_data)
# end-insert-products
# start-insert-orders
orders_coll.delete_many({})
order_data = [
{
"customer_id": "elise_smith@myemail.com",
"orderdate": datetime(2020, 5, 30, 8, 35, 52),
"product_name": "Asus Laptop",
"product_variation": "Standard Display",
"value": 431.43
},
{
"customer_id": "tj@wheresmyemail.com",
"orderdate": datetime(2019, 5, 28, 19, 13, 32),
"product_name": "The Day Of The Triffids",
"product_variation": "2nd Edition",
"value": 5.01
},
{
"customer_id": "oranieri@warmmail.com",
"orderdate": datetime(2020, 1, 1, 8, 25, 37),
"product_name": "Morphy Richards Food Mixer",
"product_variation": "Deluxe",
"value": 63.13
},
{
"customer_id": "jjones@tepidmail.com",
"orderdate": datetime(2020, 12, 26, 8, 55, 46),
"product_name": "Asus Laptop",
"product_variation": "Standard Display",
"value": 429.65
}
]
orders_coll.insert_many(order_data)
# end-insert-orders
pipeline = []
# start-embedded-pl-match1
embedded_pl = [
{
"$match": {
"$expr": {
"$and": [
{"$eq": ["$product_name", "$$prdname"]},
{"$eq": ["$product_variation", "$$prdvartn"]}
]
}
}
}
]
# end-embedded-pl-match1
# start-embedded-pl-match2
embedded_pl.append({
"$match": {
"orderdate": {
"$gte": datetime(2020, 1, 1, 0, 0, 0),
"$lt": datetime(2021, 1, 1, 0, 0, 0)
}
}
})
# end-embedded-pl-match2
# start-embedded-pl-unset
embedded_pl.append({
"$unset": ["_id", "product_name", "product_variation"]
})
# end-embedded-pl-unset
# start-lookup
pipeline.append({
"$lookup": {
"from": "orders",
"let": {
"prdname": "$name",
"prdvartn": "$variation"
},
"pipeline": embedded_pl,
"as": "orders"
}
})
# end-lookup
# start-match
pipeline.append({
"$match": {
"orders": {"$ne": []}
}
})
# end-match
# start-unset
pipeline.append({
"$unset": ["_id", "description"]
})
# end-unset
# start-run-agg
aggregation_result = products_coll.aggregate(pipeline)
# end-run-agg
for document in aggregation_result:
print(document)
finally:
client.close()