Skip to content

Commit 9bcf06b

Browse files
authored
Update 4. Aggregation Pipelines: Let the Server Do It For You.md
1 parent 28ab716 commit 9bcf06b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Introduction to MongoDB in Python/4. Aggregation Pipelines: Let the Server Do It For You.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,26 @@ pipeline = [
230230
print(list(db.laureates.aggregate(pipeline)))
231231
```
232232
[{'awardedElsewhere': 478}]
233+
234+
## 🦍 Refinement: filter out "unaffiliated" people
235+
- [x] Construct a stage added_stage that filters for laureate "prizes.affiliations.country" values that are non-empty, that is, are $in a list of the distinct values that the field takes in the collection.
236+
- [x] Insert this stage into the pipeline so that it filters out single prizes (not arrays) and precedes any test for membership in an array of countries. Recall that the first parameter to <list>.insert is the (zero-based) index for insertion.
237+
```py
238+
pipeline = [
239+
{"$match": {"gender": {"$ne": "org"}}},
240+
{"$project": {"bornCountry": 1, "prizes.affiliations.country": 1}},
241+
{"$unwind": "$prizes"},
242+
{"$addFields": {"bornCountryInAffiliations": {"$in": ["$bornCountry", "$prizes.affiliations.country"]}}},
243+
{"$match": {"bornCountryInAffiliations": False}},
244+
{"$count": "awardedElsewhere"},
245+
]
246+
247+
# Construct the additional filter stage
248+
added_stage = {"$match": {"prizes.affiliations.country": {
249+
"$in": db.laureates.distinct("prizes.affiliations.country")}}}
250+
251+
# Insert this stage into the pipeline
252+
pipeline.insert(3, added_stage)
253+
print(list(db.laureates.aggregate(pipeline)))
254+
```
255+
[{'awardedElsewhere': 252}]

0 commit comments

Comments
 (0)