In this Python MongoDB tutorial, you will learn everything about Python MongoDB Limit to fetch a specific number of documents. In the previous tutorial, we have done how to update MongoDB documents.
In our Python MongoDB find documents tutorial, we have seen how to find all MongoDB documents using the find()
method but sometimes you need to find a specific number of documents, for example, you need only five documents or ten documents, it all depends on your requirement, Then you can use limit()
method.
Headings of Contents
Python MongoDB Limit
Python PyMongo MongoDB database driver provides a method limit()
to limit the result set. The limit()
method takes one single parameter that should be an integer, and that number represents how many documents you want to return.
Example: Python MongoDB Limit
import pymongo
client = pymongo.MongoClient("mongodb://localhost:27017/")
# database
database = client["programmingfundadb"]
# collection
collection = database["employees"]
# access only three documents
result = collection.find().limit(3)
# access all the documents
for i in result:
print(i)
Output
{'_id': 1, 'name': 'Vishvajit', 'Occupation': 'Software Enginner', 'age': 30}
{'_id': 2, 'name': 'Peter', 'Occupation': 'DBA', 'age': 33}
{'_id': 3, 'name': 'Jarsi', 'Occupation': 'Machine Learning Enginner', 'age': 30}
Conclusion
So, in this article, you have seen all about how to limit the MongoDB document using Python. The limit()
method is the best method to limit the documents which return from the find() method.
I hope this article will help you, if you like this article, please share, support, and keep visiting for further Python MongoDB tutorials.
Thanks for reading….
Reference:- Click Here