How to limit mongo query in python
db.myusers.find(limit=2)
If you want to apply some condition, you can use db.myusers.find({query}, limit=2)
and to count the number of results, use db.myusers.find({query}, limit=2).count()
As specified in this question, indexed access will ignore the limit
. And count()
does not obey limit or skip by default as explained the manual. You can pass with_limit_and_skip=True
to make count()
work with limit.
print db_data.count(with_limit_and_skip=True)
Or you can iterate the cursor to see limit in effect.
for data in db.myusers.find().limit(2):
print data