0
0
MongoDBquery~10 mins

Pretty printing and cursor behavior in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Pretty printing and cursor behavior
Run find() query
Get cursor object
Iterate cursor to fetch documents
Pretty print each document
Cursor exhausted?
NoFetch next document
Yes
End
This flow shows how a MongoDB find() query returns a cursor, which you iterate to get documents, printing each in a readable format until no documents remain.
Execution Sample
MongoDB
cursor = db.collection.find({})
for doc in cursor:
    print(doc)
This code runs a query to get all documents, then prints each document one by one.
Execution Table
StepActionCursor StateDocument FetchedOutput (Pretty Printed Document)
1Run find() queryCursor created, points before first documentNone yetNo output yet
2Fetch first documentCursor at first document{_id:1, name:'Alice'}{ _id: 1, name: 'Alice' }
3Fetch second documentCursor at second document{_id:2, name:'Bob'}{ _id: 2, name: 'Bob' }
4Fetch third documentCursor at third document{_id:3, name:'Carol'}{ _id: 3, name: 'Carol' }
5No more documentsCursor exhaustedNoneNo output, loop ends
💡 Cursor has no more documents, iteration ends
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
cursorNonePoints to first documentPoints to second documentPoints to third documentExhausted (no more docs)
docNone{_id:1, name:'Alice'}{_id:2, name:'Bob'}{_id:3, name:'Carol'}None
Key Moments - 2 Insights
Why does the cursor not return all documents at once?
The cursor fetches documents one by one to save memory and allow processing large datasets. See execution_table steps 2-4 where each document is fetched individually.
What happens when the cursor reaches the last document?
When the cursor has no more documents, iteration stops as shown in execution_table step 5 where the cursor is exhausted and no output is printed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what document does the cursor point to after step 3?
A{_id:1, name:'Alice'}
B{_id:2, name:'Bob'}
C{_id:3, name:'Carol'}
DNo document, cursor exhausted
💡 Hint
Check the 'Cursor State' and 'Document Fetched' columns at step 3 in the execution_table.
At which step does the cursor become exhausted?
AStep 5
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the 'Cursor State' column in execution_table; exhaustion is noted at step 5.
If the collection had 5 documents instead of 3, how would the variable 'doc' change after step 4?
Adoc would be None
Bdoc would hold the 3rd document
Cdoc would hold the 4th document
Ddoc would hold all 5 documents
💡 Hint
Refer to variable_tracker and execution_table showing doc updates per step.
Concept Snapshot
MongoDB find() returns a cursor object.
Cursor fetches documents one at a time.
Iterate cursor to get each document.
Print documents for readable output.
Cursor ends when no documents remain.
Full Transcript
When you run a MongoDB find() query, it returns a cursor. This cursor points to the first document but does not load all documents at once. You then iterate over the cursor to fetch each document one by one. Each document can be printed in a pretty format for easy reading. The cursor moves forward with each fetch until it has no more documents, at which point the iteration ends. This approach helps manage memory and handle large data sets efficiently.