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))
Este exercício faz parte do curso
Introduction to MongoDB in Python
Instruções do exercício
- 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.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Translate cursor to aggregation pipeline
pipeline = [
{____: {____: {____: ____}}},
{____: {____: 1, ____: 1}},
{____: ____}
]
for doc in db.laureates.aggregate(pipeline):
print("{bornCountry}: {prizes}".format(**doc))