Skip to content
Related Articles

Related Articles

Improve Article

Python MongoDB – Limit Query

  • Last Updated : 26 May, 2020

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




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)

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:




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)

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:




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)

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’}

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :