Introduction to MongoDB in Python
Donny Winston
Instructor
db.laureates.find_one({"prizes.2": {"$exists": True}})
{'_id': ObjectId('5baacf97f35b632bbe12c1ad'),
'born': '0000-00-00',
'died': '0000-00-00',
'firstname': ('Comité international de la Croix Rouge'
'(International Committee of the Red Cross)'),
'gender': 'org',
'id': '482',
'prizes': [{'affiliations': [[]],
'category': 'peace',
'share': '1',
'year': '1917'},
{'affiliations': [[]], 'category': 'peace',
'share': '1', 'year': '1944'},
{'affiliations': [[]], 'category': 'peace',
'share': '2', 'year': '1963'}]}
db.laureates.distinct("gender")
['male', 'female', 'org']
count_documents
)distinct
aggregation is efficient if there is a collection index on the fielddb.laureates.find_one({"prizes.2": {"$exists": True}})
{'_id': ObjectId('5baacf97f35b632bbe12c1ad'),
'born': '0000-00-00',
'died': '0000-00-00',
'firstname': ('Comité international de la Croix Rouge'
'(International Committee of the Red Cross)'),
'gender': 'org',
'id': '482',
'prizes': [{'affiliations': [[]],
'category': 'peace',
'share': '1',
'year': '1917'},
{'affiliations': [[]], 'category': 'peace',
'share': '1', 'year': '1944'},
{'affiliations': [[]], 'category': 'peace',
'share': '2', 'year': '1963'}]}
db.laureates.distinct("prizes.category")
['physics', 'chemistry', 'peace',
'medicine', 'literature', 'economics']
['physics', 'chemistry', 'peace', 'medicine', 'literature', 'economics']
Introduction to MongoDB in Python