How many prizes were awarded to immigrants?
How many prizes were awarded to people who had no affiliation in their country of birth at the time of the award?
This exercise is part of the course
Introduction to MongoDB in Python
Exercise instructions
- In your aggregation pipeline
pipeline
, use the "gender" field to limit results to people (that is, not organizations). - Count prizes for which the laureate's "bornCountry" is not also the "country" of any of their affiliations for the prize. Be sure to use field paths (precede a field name with
"$"
) when appropriate.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
pipeline = [
# Limit results to people; project needed fields; unwind prizes
{____: {____: {"$ne": "org"}}},
{"$project": {"bornCountry": 1, "prizes.affiliations.country": 1}},
{"$unwind": "$prizes"},
# Count prizes with no country-of-birth affiliation
{"$addFields": {"bornCountryInAffiliations": {"$in": [____, "$prizes.affiliations.country"]}}},
{____: {"bornCountryInAffiliations": False}},
{"$count": "awardedElsewhere"},
]
print(list(db.laureates.aggregate(pipeline)))