Skip to content

Commit 852ab8f

Browse files
authored
Update 4. Aggregation Pipelines: Let the Server Do It For You.md
1 parent 77c0662 commit 852ab8f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,15 @@ pipeline = [
125125
for doc in db.prizes.aggregate(pipeline):
126126
print("{year}: {missing}".format(year=doc["_id"],missing=", ".join(sorted(doc["missing"]))))
127127
```
128+
## 🦍 Embedding aggregation expressions
129+
The `$expr` operator allows embedding of aggregation expressions in a normal query (or in a $match stage). Which of the following expressions counts the number of laureate documents with string-valued bornCountries when passed to `db.laureates.count_documents`?
130+
131+
You can assume (and check!) that the following is true:
132+
133+
assert all(isinstance(v, str) for v in db.laureates.distinct("bornCountry"))
134+
Possible Answers
135+
- [ ] {"bornCountry": {"$in": db.laureates.distinct("bornCountry")}}
136+
- [ ] {"$expr": {"$in": ["$bornCountry", db.laureates.distinct("bornCountry")]}}
137+
- [ ] {"$expr": {"$eq": [{"$type": "$bornCountry"}, "string"]}}
138+
- [ ] {"bornCountry": {"$type": "string"}}
139+
- [ ] All of the above

0 commit comments

Comments
 (0)