Skip to content

Commit 5387503

Browse files
committed
DOCSP-48163: Add async examples for Read pages
1 parent a235231 commit 5387503

18 files changed

+1404
-353
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# start-cursor-iterate
2+
results = collection.find()
3+
4+
async for document in results:
5+
print(document)
6+
# end-cursor-iterate
7+
8+
# start-cursor-next
9+
results = collection.find({ "name": "Dunkin' Donuts" })
10+
11+
print(await results.next())
12+
# end-cursor-next
13+
14+
# start-cursor-list
15+
results = collection.find({ "name": "Dunkin' Donuts" })
16+
17+
all_results = await to_list(results)
18+
19+
for document in all_results:
20+
print(document)
21+
# end-cursor-list
22+
23+
# start-cursor-close
24+
results = collection.find()
25+
26+
...
27+
28+
await results.close()
29+
# end-cursor-close

source/includes/cursors/tailable-cursor.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# start-tailable-cursor
12
oplog = client.local.oplog.rs
23
first = oplog.find().sort('$natural', pymongo.ASCENDING).limit(-1).next()
34
print(first)
@@ -13,4 +14,24 @@
1314

1415
# You end up here if the find() method returns no documents, or if
1516
# no new documents are added to the collection for more than 1 second.
16-
time.sleep(1)
17+
time.sleep(1)
18+
# end-tailable-cursor
19+
20+
# start-tailable-cursor-async
21+
oplog = client.local.oplog.rs
22+
first = oplog.find().sort('$natural', pymongo.ASCENDING).limit(-1).next()
23+
print(first)
24+
ts = first['ts']
25+
26+
while True:
27+
cursor = oplog.find({'ts': {'$gt': ts}},
28+
cursor_type=pymongo.CursorType.TAILABLE_AWAIT)
29+
await while async cursor.alive:
30+
async for doc in cursor:
31+
ts = doc['ts']
32+
print(doc)
33+
34+
# You end up here if the find() method returns no documents, or if
35+
# no new documents are added to the collection for more than 1 second.
36+
time.sleep(1)
37+
# end-tailable-cursor-async
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# start-project-include
2+
results = restaurants.find({ "name" : "Emerald Pub"}, {"name": 1, "cuisine": 1, "borough": 1})
3+
4+
async for restaurant in results:
5+
print(restaurant)
6+
# end-project-include
7+
8+
# start-project-include-without-id
9+
results = restaurants.find({ "name" : "Emerald Pub"}, {"_id": 0, "name": 1, "cuisine": 1, "borough": 1})
10+
11+
async for restaurant in results:
12+
print(restaurant)
13+
# end-project-include-without-id
14+
15+
# start-project-exclude
16+
results = restaurants.find({ "name" : "Emerald Pub"}, {"grades": 0, "address": 0} )
17+
18+
async for restaurant in results:
19+
print(restaurant)
20+
# end-project-exclude
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# start-open-change-stream
2+
database = client["sample_restaurants"]
3+
collection = database["restaurants"]
4+
5+
async with await collection.watch() as stream:
6+
async for change in stream:
7+
print(change)
8+
# end-open-change-stream
9+
10+
# start-update-for-change-stream
11+
database = client["sample_restaurants"]
12+
collection = database["restaurants"]
13+
14+
query_filter = { "name": "Blarney Castle" }
15+
update_operation = { '$set' :
16+
{ "cuisine": "Irish" }
17+
}
18+
19+
result = await collection.update_one(query_filter, update_operation)
20+
# end-update-for-change-stream
21+
22+
# start-change-stream-pipeline
23+
24+
change_pipeline = { "$match": { "operationType": "update" }},
25+
26+
async with await collection.watch(pipeline=change_pipeline) as stream:
27+
async for change in stream:
28+
print(change)
29+
# end-change-stream-pipeline
30+
31+
# start-change-stream-post-image
32+
database = client["sample_restaurants"]
33+
collection = database["restaurants"]
34+
35+
async with await collection.watch(full_document='updateLookup') as stream:
36+
async for change in stream:
37+
print(change)
38+
# end-change-stream-post-image
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# start-distinct
2+
results = await restaurants.distinct("borough")
3+
4+
async for restaurant in results:
5+
print(restaurant)
6+
# end-distinct
7+
8+
# start-distinct-with-query
9+
results = await restaurants.distinct("borough", {
10+
"cuisine": "Italian"
11+
})
12+
13+
async for restaurant in results:
14+
print(restaurant)
15+
# end-distinct-with-query
16+
17+
# start-distinct-with-comment
18+
results = await restaurants.distinct("name",
19+
{ "borough": "Bronx",
20+
"cuisine": "Pizza" },
21+
comment="Bronx pizza restaurants"
22+
)
23+
# end-distinct-with-comment
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# start-limit-method
2+
results = restaurants.find({ "cuisine" : "Italian"}).limit(5)
3+
4+
async for restaurant in results:
5+
print(restaurant["name"])
6+
# end-limit-method
7+
8+
# start-limit-option
9+
results = restaurants.find({ "cuisine" : "Italian"}, limit=5)
10+
11+
async for restaurant in results:
12+
print(restaurant["name"])
13+
# end-limit-option
14+
15+
# start-sort-method
16+
results = restaurants.find({ "cuisine" : "Italian"}).sort("name", pymongo.ASCENDING)
17+
18+
async for restaurant in results:
19+
print(restaurant["name"])
20+
# end-sort-method
21+
22+
# start-sort-option
23+
results = restaurants.find({ "cuisine" : "Italian"}, sort={"name": pymongo.ASCENDING} )
24+
25+
async for restaurant in results:
26+
print(restaurant["name"])
27+
# end-sort-option
28+
29+
# start-skip
30+
results = restaurants.find({ "borough" : "Manhattan"}).skip(10)
31+
32+
async for restaurant in results:
33+
print(restaurant["name"])
34+
# end-skip
35+
36+
# start-skip-option
37+
results = restaurants.find({ "borough" : "Manhattan"}, skip=10)
38+
39+
async for restaurant in results:
40+
print(restaurant["name"])
41+
# end-skip-option
42+
43+
# start-limit-sort-skip
44+
results = restaurants.find({ "cuisine" : "Italian"}) \
45+
.sort("name", pymongo.ASCENDING) \
46+
.limit(5) \
47+
.skip(10)
48+
49+
async for restaurant in results:
50+
print(restaurant["name"])
51+
# end-limit-sort-skip
52+
53+
# start-limit-sort-skip-option
54+
results = restaurants.find({ "cuisine" : "Italian"}, limit=5, sort={"name": pymongo.ASCENDING}, skip=10)
55+
56+
async for restaurant in results:
57+
print(restaurant["name"])
58+
# end-limit-sort-skip-option
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# start-sample-data
2+
from pymongo import AsyncMongoClient
3+
4+
uri = "<connection string URI>"
5+
client = AsyncMongoClient(uri)
6+
7+
try:
8+
database = client["sample_fruit"]
9+
collection = database["fruits"]
10+
11+
await collection.insert_many([
12+
{ "_id": 1, "name": "apples", "qty": 5, "rating": 3, "color": "red", "type": ["fuji", "honeycrisp"] },
13+
{ "_id": 2, "name": "bananas", "qty": 7, "rating": 4, "color": "yellow", "type": ["cavendish"] },
14+
{ "_id": 3, "name": "oranges", "qty": 6, "rating": 2, "type": ["naval", "mandarin"] },
15+
{ "_id": 4, "name": "pineapple", "qty": 3, "rating": 5, "color": "yellow" },
16+
])
17+
18+
await client.close()
19+
20+
except Exception as e:
21+
raise Exception("Error inserting documents: ", e)
22+
# end-sample-data
23+
24+
# start-find-comparison
25+
results = collection.find({ "rating": { "$gt" : 2 }})
26+
27+
async for f in results:
28+
print(f)
29+
# end-find-comparison
30+
31+
# start-find-logical
32+
results = collection.find({
33+
"$or": [
34+
{ "qty": { "$gt": 5 }},
35+
{ "color": "yellow" }
36+
]
37+
})
38+
39+
async for f in results:
40+
print(f)
41+
# end-find-logical
42+
43+
# start-find-array
44+
results = collection.find({
45+
"type" : { "$size": 2 }
46+
})
47+
48+
async for f in results:
49+
print(f)
50+
# end-find-array
51+
52+
# start-find-element
53+
results = collection.find( { "color" : { "$exists": "true" }} )
54+
55+
async for f in results:
56+
print(f)
57+
# end-find-element
58+
59+
# start-find-evaluation
60+
results = collection.find({ "name" : { "$regex" : "p{2,}" }} )
61+
62+
async for f in results:
63+
print(f)
64+
# end-find-evaluation
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# start-find-one
2+
results = await collection.find_one({ "<field name>" : "<value>" })
3+
4+
print(results)
5+
# end-find-one
6+
# start-find
7+
results = collection.find({ "<field name>" : "<value>" })
8+
9+
async for document in results:
10+
print(document)
11+
# end-find
12+
# start-count-all
13+
count = await collection.count_documents({})
14+
15+
print(count)
16+
#end-count-all
17+
# start-count-query
18+
count = await collection.count_documents({ "<field name>": "<value>" })
19+
20+
print(count)
21+
# end-count-query
22+
23+
# start-estimated-count
24+
count = await collection.estimated_document_count()
25+
26+
print(count)
27+
# end-estimated-count
28+
# start-distinct
29+
results = collection.distinct("<field name>")
30+
31+
async for document in results:
32+
print(document)
33+
# end-distinct
34+
35+
# start-watch-for-changes
36+
async with await collection.watch() as stream:
37+
async for change in stream:
38+
print(change)
39+
# end-watch-for-changes
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pymongo
2+
from pymongo import AsyncMongoClient
3+
4+
try:
5+
uri = "<connection string URI>"
6+
client = AsyncMongoClient(uri)
7+
8+
database = client["<database name>"]
9+
collection = database["<collection name>"]
10+
11+
# start example code here
12+
13+
# end example code here
14+
15+
client.close()
16+
17+
except Exception as e:
18+
raise Exception(
19+
"The following error occurred: ", e)

0 commit comments

Comments
 (0)