Pre-filtering distinct values

Introduction to MongoDB in Python

Donny Winston

Instructor

Awards into prize shares

db.laureates.find_one({"prizes.share": "4"})
{'bornCountry': 'France',
 'died': '1906-04-19',
 'diedCountry': 'France',,
 'firstname': 'Pierre',
 'prizes': [{'affiliations': [{'city': 'Paris',
   'country': 'France',
   'name': ('École municipale de physique et de chimie'
            'industrielles (Municipal School of Industrial'
            'Physics and Chemistry)')}],
   'motivation': ('"in recognition of the extraordinary'
                  'services they have rendered by their'
                  'joint researches on the radiation'
                  'phenomena discovered by Professor'
                  'Henri Becquerel"'),
   'category': 'physics', 'share': '4', 'year': '1903'}],
 'surname': 'Curie',
 ...
}
Introduction to MongoDB in Python

High-share prize categories

db.laureates.distinct("prizes.category")
['physics', 'chemistry', 'peace',
 'medicine', 'literature', 'economics']
list(db.laureates.find({"prizes.share": "4"}))
[...]
db.laureates.distinct(
  "prizes.category", {"prizes.share": '4'})
['physics', 'chemistry', 'medicine']
db.prizes.distinct("category", {"laureates.share": "4"})
['physics', 'medicine', 'chemistry']
Introduction to MongoDB in Python

Prize categories with multi-winners

db.laureates.count_documents({"prizes.1": {"$exists": True}})
6
db.laureates.distinct(
  "prizes.category", {"prizes.1": {"$exists": True}})
['chemistry', 'physics', 'peace']
# We'll learn how to do this in the next chapter:
[[{'category': 'physics'}, {'category': 'chemistry'}],
 [{'category': 'physics'}, {'category': 'physics'}],
 [{'category': 'chemistry'}, {'category': 'peace'}],
 [{'category': 'chemistry'}, {'category': 'chemistry'}],
 [{'category': 'peace'}, {'category': 'peace'},
  {'category': 'peace'}],
 [{'category': 'peace'}, {'category': 'peace'}]]
Introduction to MongoDB in Python

Practice time!

Introduction to MongoDB in Python

Preparing Video For Download...