-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path2_16_cursor_results.py
More file actions
36 lines (27 loc) · 973 Bytes
/
Copy path2_16_cursor_results.py
File metadata and controls
36 lines (27 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from pymongo import MongoClient
import time
from dotenv import load_dotenv
import os
load_dotenv()
client = MongoClient(os.getenv("DRIVERS_CONNECTION_STRING"))
db = client["test"]
collection = db["test_cursor_fetch"]
# If collection is empty, create 10,000 test documents
if collection.count_documents({}) == 0:
collection.insert_many(
[{"name": f"Student {i}", "age": i % 30} for i in range(10000)]
)
print("Inserted 10,000 documents")
# 1. Fetch all documents using list(cursor)
start_time = time.time()
docs = list(collection.find()) # Fetches all at once
end_time = time.time()
print(f"list(cursor) took {end_time - start_time:.5f} seconds")
# 2. Fetch all documents using for loop
def process_doc(doc):
return doc["name"]
start_time = time.time()
for doc in collection.find(): # Fetches documents one by one with some processing
process_doc(doc)
end_time = time.time()
print(f"for loop took {end_time - start_time:.5f} seconds")