Skip to content

Commit de1504d

Browse files
authored
Update 3. Get Only What You Need, and Fast.md
1 parent fdc074e commit de1504d

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,13 @@ for doc in docs[:5]:
108108
{'born': '1901-02-28', 'prizes': [{'year': '1954'}, {'year': '1962'}]}
109109
{'born': '1913-07-12', 'prizes': [{'year': '1955'}]}
110110
{'born': '1911-01-26', 'prizes': [{'year': '1955'}]}
111-
Possible Answers
111+
Possible Answers :
112112
- [x] [("prizes.year", 1), ("born", -1)]
113113
- [ ] {"prizes.year": 1, "born": -1}
114114
- [ ] None
115115
- [ ] [("prizes.year", 1)]
116116

117-
answer code
117+
answer code :
118118
```py
119119
docs = list(db.laureates.find(
120120
{"born": {"$gte": "1900"}, "prizes.year": {"$gte": "1954"}},
@@ -123,3 +123,21 @@ docs = list(db.laureates.find(
123123
for doc in docs[:5]:
124124
print(doc)
125125
```
126+
127+
## 🦍 Sorting together: MongoDB + Python
128+
> You will use Python to sort laureates for one prize by last name, and then MongoDB to sort prizes by year:
129+
130+
1901: Röntgen
131+
1902: Lorentz and Zeeman
132+
1903: Becquerel and Curie and Curie, née Sklodowska
133+
You'll start by writing a function that takes a prize document as an argument, extracts all the laureates from that document, arranges them in alphabetical order, and returns a string containing the last names separated by " and "
134+
135+
The Nobel database is again available to you as db. We also pre-loaded a sample document sample_doc so you can test your laureate-extracting function.
136+
(Remember that you can always type help(function_name) in console to get a refresher on functions you might be less familiar with, e.g. help(sorted)!)
137+
138+
---
139+
Complete the definition of all_laureates(prize). Within the body of the function:
140+
- [x] Sort the "laureates" list of the prize document according to the "surname" key.
141+
- [x] For each of the laureates in the sorted list, extract the "surname" field.
142+
- [x] The code for joining the last names into a single string is already written for you.
143+
- [x] Take a look at the console to make sure the output looks like what you'd expect!

0 commit comments

Comments
 (0)