Python MongoDB Tutorial
Selected Reading
- Python MongoDB - Discussion
- Python MongoDB - Useful Resources
- Python MongoDB - Quick Guide
- Python MongoDB - Limit
- Python MongoDB - Update
- Python MongoDB - Drop Collection
- Python MongoDB - Delete Document
- Python MongoDB - Sort
- Python MongoDB - Query
- Python MongoDB - Find
- Python MongoDB - Insert Document
- Python MongoDB - Create Collection
- Python MongoDB - Create Database
- Python MongoDB - Introduction
- Python MongoDB - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python MongoDB - Limit
Python MongoDB - Limit
While retrieving the contents of a collection you can pmit the number of documents in the result using the pmit() method. This method accepts a number value representing the number of documents you want in the result.
Syntax
Following is the syntax of the pmit() method −
>db.COLLECTION_NAME.find().pmit(NUMBER)
Example
Assume we have created a collection and inserted 5 documents into it as shown below −
> use testDB switched to db testDB > db.createCollection("sample") { "ok" : 1 } > data = [ ... {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"}, ... {"_id": "1002", "name": "Rahim", "age": 27, "city": "Bangalore"}, ... {"_id": "1003", "name": "Robert", "age": 28, "city": "Mumbai"}, ... {"_id": "1004", "name": "Romeo", "age": 25, "city": "Pune"}, ... {"_id": "1005", "name": "Sarmista", "age": 23, "city": "Delhi"}, ... {"_id": "1006", "name": "Rasajna", "age": 26, "city": "Chennai"} ] > db.sample.insert(data) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 6, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
Following pne retrieves the first 3 documents of the collection.
> db.sample.find().pmit(3) { "_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Hyderabad" } { "_id" : "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore" } { "_id" : "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" }
Limiting the Documents Using Python
To restrict the results of a query to a particular number of documents pymongo provides the pmit() method. To this method pass a number value representing the number of documents you need in the result.
Example
Following example retrieves first three documents in a collection.
from pymongo import MongoCpent #Creating a pymongo cpent cpent = MongoCpent( localhost , 27017) #Getting the database instance db = cpent[ l ] #Creating a collection coll = db[ myColl ] #Inserting document into a collection data = [ {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"}, {"_id": "1002", "name": "Rahim", "age": "27", "city": "Bangalore"}, {"_id": "1003", "name": "Robert", "age": "28", "city": "Mumbai"}, {"_id": "1004", "name": "Romeo", "age": 25, "city": "Pune"}, {"_id": "1005", "name": "Sarmista", "age": 23, "city": "Delhi"}, {"_id": "1006", "name": "Rasajna", "age": 26, "city": "Chennai"} ] res = coll.insert_many(data) print("Data inserted ......") #Retrieving first 3 documents using the find() and pmit() methods print("First 3 documents in the collection: ") for doc1 in coll.find().pmit(3): print(doc1)
Output
Data inserted ...... First 3 documents in the collection: { _id : 1001 , name : Ram , age : 26 , city : Hyderabad } { _id : 1002 , name : Rahim , age : 27 , city : Bangalore } { _id : 1003 , name : Robert , age : 28 , city : Mumbai }Advertisements