In this example we will show how to limit the result of a query using the limit() method in Python MongoDB.
Source Code
from pymongo import MongoClient
client = MongoClient("localhost", 27017)
db = client["mydatabase"]
collection = db["us_state"]
for x in collection.find().limit(4):
print(x)
Output:
{'_id': 1, 'State_name': 'Alabama', 'Abbreviation': 'AL', 'Capital': 'Montgomery', 'Total_area': 52420}
{'_id': 2, 'State_name': 'Alaska', 'Abbreviation': 'AK', 'Capital': 'Juneau', 'Total_area': 665384}
{'_id': 3, 'State_name': 'Arizona', 'Abbreviation': 'AZ', 'Capital': 'Phoenix', 'Total_area': 113990}
{'_id': 4, 'State_name': 'Arkansas', 'Abbreviation': 'AR', 'Capital': 'Little Rock', 'Total_area': 53179}