Skip to content

Commit 927269b

Browse files
authored
Update 2. Working with Distinct Values and Sets.md
1 parent d091cd7 commit 927269b

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

Introduction to MongoDB in Python/2. Working with Distinct Values and Sets.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,52 @@ Possible Answers
159159
```py
160160
db.laureates.count_documents({"firstname": Regex("^G"), "surname": Regex("^S")})
161161
```
162+
163+
## Germany, then and now
164+
- [x] 1. Use a regular expression object to filter for laureates with "Germany" in their "bornCountry" value.
165+
- [x] 2. Use a regular expression object to filter for laureates with a "bornCountry" value starting with "Germany".
166+
- [x] 3. Use a regular expression object to filter for laureates born in what was at the time Germany but is now another country.
167+
- [x] 4. Use a regular expression object to filter for laureates born in what is now Germany but at the time was another country.
168+
```py
169+
from bson.regex import Regex
170+
171+
# Filter for laureates with "Germany" in their "bornCountry" value
172+
criteria = {"bornCountry": Regex("Germany")}
173+
print(set(db.laureates.distinct("bornCountry", criteria)))
174+
```
175+
```py
176+
from bson.regex import Regex
177+
178+
# Filter for laureates with a "bornCountry" value starting with "Germany"
179+
criteria = {"bornCountry": Regex("^Germany")}
180+
print(set(db.laureates.distinct("bornCountry", criteria)))
181+
```
182+
```py
183+
from bson.regex import Regex
184+
185+
# Fill in a string value to be sandwiched between the strings "^Germany " and "now"
186+
criteria = {"bornCountry": Regex("^" + "Germany \(" + "now")}
187+
print(set(db.laureates.distinct("bornCountry", criteria)))
188+
```
189+
```py
190+
from bson.regex import Regex
191+
192+
#Filter for currently-Germany countries of birth. Fill in a string value to be sandwiched between the strings "now" and "$"
193+
criteria = {"bornCountry": Regex("now" + " Germany\)" + "$")}
194+
print(set(db.laureates.distinct("bornCountry", criteria)))
195+
```
196+
197+
## The prized transistor
198+
- [x] Save a filter criteria that finds laureates with prizes.motivation values containing "transistor" as a substring. The substring can appear anywhere within the value, so no anchoring characters are needed.
199+
- [x] Save to first and last the field names corresponding to a laureate's first name and last name (i.e. "surname") so that we can print out the names of these laureates.
200+
```py
201+
from bson.regex import Regex
202+
203+
# Save a filter for laureates with prize motivation values containing "transistor" as a substring
204+
criteria = {"prizes.motivation": Regex("transistor")}
205+
206+
# Save the field names corresponding to a laureate's first name and last name
207+
first, last = 'firstname', 'surname'
208+
print([(laureate[first], laureate[last])
209+
for laureate in db.laureates.find(criteria)])
210+
```

0 commit comments

Comments
 (0)