Skip to content

Commit fa09cfa

Browse files
authored
Update 3. Get Only What You Need, and Fast.md
1 parent 6d4ae60 commit fa09cfa

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Introduction to MongoDB in Python/3. Get Only What You Need, and Fast.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,24 @@ docs = db.prizes.find(
173173
for doc in docs:
174174
print("{year}: {names}".format(year=doc['year'], names=all_laureates(doc)))
175175
```
176+
177+
## 🦍 Gap years
178+
- [x] Find the original prize categories established in 1901 by looking at the distinct values of the "category" field for prizes from year 1901.
179+
- [x] Fetch ONLY the year and category from all the documents (without the "_id" field).
180+
- [x] Sort by "year" in descending order, then by "category" in ascending order.
181+
```py
182+
# original categories from 1901
183+
original_categories = db.prizes.distinct('category', {'year': '1901'})
184+
print(original_categories)
185+
186+
# project year and category, and sort
187+
docs = db.prizes.find(
188+
filter={},
189+
projection = {'year':1, 'category':1, "_id":0},
190+
sort=[("year", -1), ("category", 1)]
191+
)
192+
193+
#print the documents
194+
for doc in docs:
195+
print(doc)
196+
```

0 commit comments

Comments
 (0)