Aggregating a few individuals' country data
The following query cursor yields birth-country and prize-affiliation-country information for three non-organization laureates:
cursor = (db.laureates.find(
{"gender": {"$ne": "org"}},
["bornCountry", "prizes.affiliations.country"]
).limit(3))
This exercise is part of the course
Introduction to MongoDB in Python
Exercise instructions
- Translate the above cursor
cursor
to an equivalent aggregation cursor, saving the pipeline stages topipeline
. Recall that thefind
collection method's "filter" parameter maps to the "$match" aggregation stage, its "projection" parameter maps to the "$project" stage, and the "limit" parameter (or cursor method) maps to the "$limit" stage.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Translate cursor to aggregation pipeline
pipeline = [
{____: {____: {____: ____}}},
{____: {____: 1, ____: 1}},
{____: ____}
]
for doc in db.laureates.aggregate(pipeline):
print("{bornCountry}: {prizes}".format(**doc))