Skip to content

Commit 401316f

Browse files
authored
Update 3. Get Only What You Need, and Fast.md
1 parent 25304b0 commit 401316f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,26 @@ You want to examine the laureates of the 1903 prize in physics and how they spli
1313
db.laureates.find_one({"prizes": {"$elemMatch": {"category": "physics", "year": "1903"}}})
1414

1515
> Which projection(s) will fetch ***ONLY*** the laureates' full names and prize share info? I encourage you to experiment with the console and re-familiarize yourself with the structure of laureate collection documents.
16+
- [ ] ["firstname", "surname", "prizes"]
17+
- [ ] ["firstname", "surname", "prizes.share"]
18+
- [x] {"firstname": 1, "surname": 1, "prizes.share": 1, "_id": 0}
19+
- [ ] All of the above
20+
21+
## 🦍 Rounding up the G.S. crew
22+
{ "name": {$regex: "^Py"} }
23+
will fetch documents where the field 'name' ***starts with "Py".***
24+
25+
> In this exercise, you will use regular expressions, projection, and list comprehension to collect the full names of laureates whose initials are "G.S.".
26+
27+
- [x] 1. First, use regular expressions to fetch the documents for the laureates whose "firstname" starts with "G" and whose "surname" starts with "S".
28+
- [x] 2. Use projection and adjust the query to select only the "firstname" and "surname" fields.
29+
30+
```py
31+
# Find laureates whose first name starts with "G" and last name starts with "S"
32+
docs = db.laureates.find(
33+
filter= {"firstname" : {"$regex" : "^G"},
34+
"surname" : {"$regex" : "^S"} })
35+
# Print the first document
36+
print(docs[0])
37+
```
38+
```py

0 commit comments

Comments
 (0)