Introduction to MongoDB in Python
Donny Winston
Instructor
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',
...
}
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']
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