SwadhyAI Notes

All Python notes

MongoDB Limit 🛑

Limit the Result

To limit the result in MongoDB, we use the limit() method.

✨ Limit Result

The limit() method takes one parameter, a number defining how many documents to return.

python
import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]

myresult = mycol.find().limit(5)

#print the result:
for x in myresult:
  print(x)

💡 Did You Know?

The limit() method is very useful when you have large collections and only want to return a specific number of documents.