Thursday, July 11, 2024
HomeLanguagesPythonPython MongoDB – Limit Query

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. PyMongo is the Python driver for mongoDB.

Limit() Method: 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

Sample Database is as follows:

python-mongodb-sample-database2

Example 1: 

Python3




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(& quot
       First 3 docs in the collection are: & quot
       )
 
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 specified criteria.

Example 2: 

Python3




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: 

Python3




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(& quot
       3 docs in the collection are: & quot
       )
 
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’}

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments