File tree Expand file tree Collapse file tree 4 files changed +22
-18
lines changed
Expand file tree Collapse file tree 4 files changed +22
-18
lines changed Original file line number Diff line number Diff line change 1919
2020# start-tailable-cursor-async
2121oplog = client .local .oplog .rs
22- first = oplog .find ().sort ('$natural' , pymongo .ASCENDING ).limit (- 1 ).next ()
22+ first = await oplog .find ().sort ('$natural' , pymongo .ASCENDING ).limit (- 1 ).next ()
2323print (first )
2424ts = first ['ts' ]
2525
2626while True :
2727 cursor = oplog .find ({'ts' : {'$gt' : ts }},
2828 cursor_type = pymongo .CursorType .TAILABLE_AWAIT )
29- await while async cursor .alive :
29+ while cursor .alive :
3030 async for doc in cursor :
3131 ts = doc ['ts' ]
3232 print (doc )
3333
3434 # You end up here if the find() method returns no documents, or if
3535 # no new documents are added to the collection for more than 1 second.
36- time .sleep (1 )
36+ await asyncio .sleep (1 )
3737# end-tailable-cursor-async
Original file line number Diff line number Diff line change 11# start-distinct
22results = await restaurants .distinct ("borough" )
33
4- async for restaurant in results :
4+ for restaurant in results :
55 print (restaurant )
66# end-distinct
77
1010 "cuisine" : "Italian"
1111})
1212
13- async for restaurant in results :
13+ for restaurant in results :
1414 print (restaurant )
1515# end-distinct-with-query
1616
Original file line number Diff line number Diff line change 2626print (count )
2727# end-estimated-count
2828# start-distinct
29- results = collection .distinct ("<field name>" )
29+ results = await collection .distinct ("<field name>" )
3030
31- async for document in results :
31+ for document in results :
3232 print (document )
3333# end-distinct
3434
Original file line number Diff line number Diff line change 1+ import asyncio
12import pymongo
23from pymongo import AsyncMongoClient
34
4- try :
5- uri = "<connection string URI>"
6- client = AsyncMongoClient (uri )
5+ async def main ():
6+ try :
7+ uri = "<connection string URI>"
8+ client = AsyncMongoClient (uri )
79
8- database = client ["<database name>" ]
9- collection = database ["<collection name>" ]
10+ database = client ["<database name>" ]
11+ collection = database ["<collection name>" ]
1012
11- # start example code here
13+ # start example code here
1214
13- # end example code here
15+ # end example code here
1416
15- client .close ()
17+ await client .close ()
1618
17- except Exception as e :
18- raise Exception (
19- "The following error occurred: " , e )
19+ except Exception as e :
20+ raise Exception (
21+ "The following error occurred: " , e )
22+
23+ asyncio .run (main ())
You can’t perform that action at this time.
0 commit comments