

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Is there a way to limit the number of records in a certain MongoDB collection?
Yes, you can use capped parameter along with max size. Following is the syntax −
db.createCollection("yourCollectionName",{capped:true,size:yourSizeInBytes,max:howManyRecordsYouWant})
Let us first create a collection with capped:true −
> db.createCollection("limitTheNumberOfRecordsDemo",{capped:true,size:200024,max:3}) { "ok" : 1 }
We will now create a collection with documents −
> db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"James Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e601b50a6c6dd317adad") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Sam Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e60bb50a6c6dd317adae") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e612b50a6c6dd317adaf") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Carol Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e61ab50a6c6dd317adb0") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"Adam Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e649b50a6c6dd317adb1") } > db.limitTheNumberOfRecordsDemo.insertOne({"ClientName":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5cd9e64fb50a6c6dd317adb2") }
Following is the query to display all documents from a collection with the help of find() method. Here you will get only 3 records from the above collection since we have set the capped with max size above −
> db.limitTheNumberOfRecordsDemo.find();
This will produce the following output −
{ "_id" : ObjectId("5cd9e61ab50a6c6dd317adb0"), "ClientName" : "Carol Taylor" } { "_id" : ObjectId("5cd9e649b50a6c6dd317adb1"), "ClientName" : "Adam Smith" } { "_id" : ObjectId("5cd9e64fb50a6c6dd317adb2"), "ClientName" : "John Doe" }
- Related Questions & Answers
- Limit the number of documents in a collection in MongoDB?
- MongoDB Aggregate to limit the number of records
- How to limit the number of records, while retrieving data from a MongoDB collection using Java?
- Is there a way to list collections in MongoDB?
- In MySQL, is there a way to turn column records into a list?
- Is there any way to see the MongoDB results in a better format?
- Is there a way to subtract number of days from a date in MySQL?
- Deleting all records of a collection in MongoDB Shell?
- Is there any easy way to add multiple records in a single MySQL query?
- Is there any way to skip some documents in MongoDB?
- Limit number of values in a field using MongoDB?
- Limit number of values in a field with MongoDB?
- Is there any way in MongoDB to get the inner value of json data?
- Count number of rows in a MongoDB collection
- How to count the number of documents in a MongoDB collection?
Advertisements