Last Updated : 25 Jul, 2025
When working with MongoDB in Python using PyMongo, you typically retrieve data from a collection using the find() method. But instead of getting all the matching documents at once, PyMongo returns something called a cursor.
A PyMongo cursor is a special iterable object that acts like a pointer to the result set of a MongoDB query. It allows you to fetch and process query results one document at a time rather than loading everything into memory at once.
Why do we need CursorUsing a cursor provides several important advantages, especially when dealing with large datasets:
You can fine-tune the data returned by the cursor using:
Sample database is as follows:
Example: Using Cursor in PyMongo
javascript
from pymongo import MongoClient
# Connecting to mongodb
client = MongoClient('mongodb://localhost:27017/')
with client:
db = client.GFG
lectures = db.lecture.find()
print(lectures.next())
print(lectures.next())
print(lectures.next())
print("\nRemaining Lectures\n")
print(list(lectures))
Output:
Explanation:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4