Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Nested Queries in PyMongo
Next article icon

In PyMongo, thelimit() method is used to restrict the number of documents returned by a query. It’s especially useful when you want to preview only a few records from a large dataset.

Syntax

collection.find().limit(n)

Parameter: n (int) is the maximum number of documents to return.

Here is our sample data.

Python
frompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c['grpDB']col=db['sales']data=[{"_id":1,"user":"Amit","product":"Pen","amount":5},{"_id":2,"user":"Drew","product":"Pencil","amount":3},{"_id":3,"user":"Amit","product":"Notebook","amount":15},{"_id":4,"user":"Cody","product":"Pen","amount":7},{"_id":5,"user":"Drew","product":"Notebook","amount":12},{"_id":6,"user":"Cody","product":"Eraser","amount":2},{"_id":7,"user":"Amit","product":"Pen","amount":10}]col.delete_many({})col.insert_many(data)print("Data inserted.")

Output

Data inserted.

Sample_data
Output
Sample data

Explanation:

  • MongoClient() connects to the MongoDB server running on localhost at the default port 27017.
  • delete_many({}) clears any existing documents from the collection to prevent duplicate entries.
  • insert_many(data)inserts the list of sample documents into the collection.

Examples

Example 1: Limit to first 3 documents

Python
#Driver Code StartsfrompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c['grpDB']col=db['sales']#Driver Code Endsres=col.find().limit(3)fordocinres:#Driver Code Startsprint(doc)#Driver Code Ends

Output

Output
First 3 docs

Explanation: find().limit(3)returns only the first 3 documents from the collection.

Example 2: Combine with sort

Python
frompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c['grpDB']col=db['sales']res=col.find().sort("amount",-1).limit(2)fordocinres:print(doc)

Output

Output
Top 2 by amount

Explanation: Sorts the documents by amount in descending order and limits the output to the top 2.

Example 3: Limit results based on a filter

Python
frompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c['grpDB']col=db['sales']res=col.find({"user":"Cody"}).limit(1)fordocinres:print(doc)

Output

Output
First Cody record

Explanation: Filters documents where user is "Cody" and limits the result to just 1 document (the first match based on insertion order).

Related Articles


https://media.geeksforgeeks.org/auth/avatar.png
GeeksforGeeks
Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp