Limit Query in MongoDB Python

Shashank Shanu

9 months ago

MongoDB Python
MongoDB Python
While working with any project, you might come across a scenario where you wanted to retrieve only top 5 data, last 3 data or you might want data from the middle of your dataset. Today, in this article I will try to show you how we can do this MongoDB.
First lets us recap.

What is MongoDB?

MongoDB is one of the widely used databases with its document stored as collections. These documents can be compared to JSON objects. It is classified as a NoSQL database program.
Note: MongoDB when it is used with Python then the combination is termed as PyMongo.

MongoDB: Limit () function

The limit () function does what its name suggests. It helps us in limiting the number of documents that will be returned. It takes only one argument as the parameter which represents the number signifying the number of documents that need to be returned.
Syntax of the limit function

Let try to understand it better with the help of an example:
Let suppose, we have created a collection and inserted 5 documents into it as shown below −

# using database

use my_test_DB


data = [
   ... {"_id":
"101", "name": "Shyam", "age":
"27", "city": "Hyderabad"},
   ... {"_id":
"102", "name": "Ram", "age": 28,
"city": "Bangalore"},
   ... {"_id":
"103", "name": "john", "age": 26,
"city": "Mumbai"},
   ... {"_id":
"104", "name": "Romeo", "age": 25,
"city": "Pune"},
   ... {"_id":
"105", "name": "Sarmista", "age": 23,
"city": "Delhi"},
   ... {"_id":
"106", "name": "Rasajna", "age": 26,
"city": "Chennai"}

#insert data

   "writeErrors" : [ ],
   "writeConcernErrors" : [ ],
   "nInserted" : 6,
   "nUpserted" : 0,
   "nMatched" : 0,
   "nModified" : 0,
   "nRemoved" : 0,
   "upserted" : [ ]

Now suppose you want to retrieve only first 3 documents form the above-created collection. This can be done as shown below:

The first three documents are as follows:
{ "_id" : "101", "name" : "Shyam", "age" : "27", "city" : "Hyderabad" }
{ "_id" : "102", "name" : "Ram", "age" : 28, "city" : "Bangalore" }
{ "_id" : "103", "name" : "john", "age" : 26, "city" : "Mumbai" }

Till now, we saw how we can limit our file in MongoDB, next lets see how we can do it using python.

Limiting the Documents Using Python

For limiting mongo DB file using python is same as we discuss above. Only we have to import Mongo Client from pymongo and connect and then whatever function are available in mongo DB we can use them here also.
Lets see an example:
Here, we are trying to retrieve first three documents in a collection.

from pymongo import

#Create a pymongo client
mo_client = MongoClient('localhost', 27017)

#Getting the database instance
database = mo_client ['l']

#Creating a collection
collection = database ['my_Collections']

#Inserting document into a collection
data = [
   {"_id": "1001",
"name": "Shyam", "age": "27",
"city": "Hyderabad"},
   {"_id": "1002",
"name": "Ram", "age": "28",
"city": "Bangalore"},
   {"_id": "1003",
"name": "John", "age": "26",
"city": "Mumbai"},
{"_id": "1004", "name": "Romeo",
"age": 25, "city": "Pune"},
   {"_id": "1005",
"name": "Sarmista", "age": 23, "city":
   {"_id": "1006",
"name": "Rasajna", "age": 26, "city":
result = collection.insert_many(data)
print("Data inserted ......")

#Retrieving first 3 documents using the find() and limit() methods

print("First 3 documents in the collection: ")
for i in coll.find().limit(3):
Data inserted ......
First 3 documents in the collection:
{'_id': '101', 'name': 'Shyam', 'age': '27', 'city': 'Hyderabad'}
{'_id': '102', 'name': 'Ram', 'age': '28', 'city': 'Bangalore'}
{'_id': '103', 'name': 'John', 'age': '26', 'city': 'Mumbai'}
From the above result, we can observe that we are getting what we wanted i.e., first three collection of the documents.
I hope after you enjoyed reading this article and finally, you came to know about Limit Query in MongoDB Python.
For more such blogs/courses on data science, machine learning, artificial intelligence and emerging new technologies do visit us at InsideAIML.
Thanks for reading…
Happy Programming…

Submit Review