Intro to Aggregation: From Query Components to Aggregation Stages

Introduction to MongoDB in Python

Donny Winston

Instructor

Queries have implicit stages

cursor = db.laureates.find(
  filter={"bornCountry": "USA"},
  projection={"prizes.year": 1},
  limit=3
)
for doc in cursor:
  print(doc["prizes"])
[{'year': '1923'}]
[{'year': '1927'}]
[{'year': '1936'}]
cursor = db.laureates.aggregate([
  stage_1,
  stage_2,
  ...
])
cursor = db.laureates.aggregate([
  {"$match": {"bornCountry": "USA"}},
  {"$project": {"prizes.year": 1}},
  {"$limit": 3}
])
for doc in cursor:
  print(doc["prizes"])
[{'year': '1923'}]
[{'year': '1927'}]
[{'year': '1936'}]
Introduction to MongoDB in Python

Adding sort and skip stages

from collections import OrderedDict

list(db.laureates.aggregate([
    {"$match": {"bornCountry": "USA"}},
    {"$project": {"prizes.year": 1, "_id": 0}},
    {"$sort": OrderedDict([("prizes.year", 1)])},
    {"$skip": 1},
    {"$limit": 3}
]))
[{'prizes': [{'year': '1912'}]},
 {'prizes': [{'year': '1914'}]},
 {'prizes': [{'year': '1919'}]}]
Introduction to MongoDB in Python

But can I count?

list(db.laureates.aggregate([
    {"$match": {"bornCountry": "USA"}},
    {"$count": "n_USA-born-laureates"}
]))
[{'n_USA-born-laureates': 269}]
db.laureates.count_documents({"bornCountry": "USA"})
269

What about db.laureates.distinct("bornCountry")?

Introduction to MongoDB in Python

Let's practice!

Introduction to MongoDB in Python

Preparing Video For Download...