Skip to content

Commit 455e4b0

Browse files
authored
Update 4. Aggregation Pipelines: Let the Server Do It For You.md
1 parent 4dfafbb commit 455e4b0

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Introduction to MongoDB in Python/4. Aggregation Pipelines: Let the Server Do It For You.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,20 @@ pipeline = [
191191
]
192192
for doc in db.prizes.aggregate(pipeline): print(doc)
193193
```
194+
## 🦍 "...it's the life in your years"
195+
For the pipeline we developed in the last slide deck, I want you to replace the last ($bucket) stage with one such that, given the documents docs collected, we can get the following output:
196+
197+
from operator import itemgetter
198+
199+
print(max(docs, key=itemgetter("years")))
200+
print(min(docs, key=itemgetter("years")))
201+
202+
{'firstname': 'Rita', 'surname': 'Levi-Montalcini', 'years': 103.0}
203+
{'firstname': 'Martin Luther', 'surname': 'King Jr.', 'years': 39.0}
204+
You may assume that any earlier $project stage has been replaced by an equivalent $addFields stage
205+
206+
Possible Answers
207+
- [x] {"$project": {"years": 1, "firstname": 1, "surname": 1, "_id": 0}}
208+
- [ ] {"$addFields": {"firstname": 1, "surname": 1}}
209+
- [ ] {"$project": {"firstname": 1, "surname": 1}}
210+
- [ ] {"$project": {"firstname": 1, "surname": 1, "_id": 0}}

0 commit comments

Comments
 (0)