Python MongoDB – Limit Query

MongoDB is one of the most used databases with its document stored as collections. These documents can be compared to JSON objects. MongoDB when it is used with Python then the combination is termed as PyMongo.

Limit()

The function limit() does what its name suggests- limiting the number of documents that will be returned. There is only one argument in the parameter which is a number signifying the number of documents that need to be returned.

Syntax:

coll.find().limit(n)

where,

  • coll- name of the collection
  • n- number that needs to be returned

Example 1:



Sample Database:

python-mongodb-sample-database2

filter_none

edit
close

play_arrow

link
brightness_4
code

from pymongo import MongoClient
  
# Create a pymongo client
client = MongoClient('localhost', 27017)
  
# database instance
db = client['GFG']
  
# collection instance
doc = db['Student']
  
# Retrieving first 3 documents using the
# find() and limit() methods
print("First 3 docs in the collection are: ")
  
for doc1 in doc.find().limit(3):
    print(doc1)

chevron_right


Output:

First 3 docs in the collection are:
{‘_id’: 1, ‘name’: ‘Vishwash’, ‘Roll No’: ‘1001’, ‘Branch’: ‘CSE’}
{‘_id’: 2, ‘name’: ‘Vishesh’, ‘Roll No’: ‘1002’, ‘Branch’: ‘IT’}
{‘_id’: 3, ‘name’: ‘Shivam’, ‘Roll No’: ‘1003’, ‘Branch’: ‘ME’}

While limit() puts a limitation on the number of documents fetched, find() can be used to find documents according to some specifies criteria.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

from pymongo import MongoClient
  
# Create a pymongo client
client = MongoClient('localhost', 27017)
  
# database instance
db = client['GFG']
  
# collection instance
doc = db['Student']
  
# Printing documents of only those having
# branch as CSE and limiting the document 
# to 1
for doc1 in doc.find({'Branch': 'CSE'}).limit(1):
    print(doc1)

chevron_right


Output:

{‘_id’: 1, ‘name’: ‘Vishwash’, ‘Roll No’: ‘1001’, ‘Branch’: ‘CSE’}

For skipping some files before fetching the said amount of documents skip() can be used with limit()

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

from pymongo import MongoClient
  
# Create a pymongo client
client = MongoClient('localhost', 27017)
  
# database instance
db = client['GFG']
  
# collection instance
doc = db['Student']
  
# Retrieving 3 documents using the
# find() and limit() methods
print("3 docs in the collection are: ")
  
for doc1 in doc.find().limit(3).skip(2):
    print(doc1)

chevron_right


Output:

3 docs in the collection are:
{‘_id’: 3, ‘name’: ‘Shivam’, ‘Roll No’: ‘1003’, ‘Branch’: ‘ME’}
{‘_id’: 4, ‘name’: ‘Yash’, ‘Roll No’: ‘1004’, ‘Branch’: ‘ECE’}
{‘_id’: 5, ‘name’: ‘Raju’, ‘Roll No’: ‘1005’, ‘Branch’: ‘CSE’}




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.