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 19
19
20
20
# start-tailable-cursor-async
21
21
oplog = 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 ()
23
23
print (first )
24
24
ts = first ['ts' ]
25
25
26
26
while True :
27
27
cursor = oplog .find ({'ts' : {'$gt' : ts }},
28
28
cursor_type = pymongo .CursorType .TAILABLE_AWAIT )
29
- await while async cursor .alive :
29
+ while cursor .alive :
30
30
async for doc in cursor :
31
31
ts = doc ['ts' ]
32
32
print (doc )
33
33
34
34
# You end up here if the find() method returns no documents, or if
35
35
# no new documents are added to the collection for more than 1 second.
36
- time .sleep (1 )
36
+ await asyncio .sleep (1 )
37
37
# end-tailable-cursor-async
Original file line number Diff line number Diff line change 1
1
# start-distinct
2
2
results = await restaurants .distinct ("borough" )
3
3
4
- async for restaurant in results :
4
+ for restaurant in results :
5
5
print (restaurant )
6
6
# end-distinct
7
7
10
10
"cuisine" : "Italian"
11
11
})
12
12
13
- async for restaurant in results :
13
+ for restaurant in results :
14
14
print (restaurant )
15
15
# end-distinct-with-query
16
16
Original file line number Diff line number Diff line change 26
26
print (count )
27
27
# end-estimated-count
28
28
# start-distinct
29
- results = collection .distinct ("<field name>" )
29
+ results = await collection .distinct ("<field name>" )
30
30
31
- async for document in results :
31
+ for document in results :
32
32
print (document )
33
33
# end-distinct
34
34
Original file line number Diff line number Diff line change
1
+ import asyncio
1
2
import pymongo
2
3
from pymongo import AsyncMongoClient
3
4
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 )
7
9
8
- database = client ["<database name>" ]
9
- collection = database ["<collection name>" ]
10
+ database = client ["<database name>" ]
11
+ collection = database ["<collection name>" ]
10
12
11
- # start example code here
13
+ # start example code here
12
14
13
- # end example code here
15
+ # end example code here
14
16
15
- client .close ()
17
+ await client .close ()
16
18
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