Home >>MongoDB Tutorial >MongoDB Limit Records
You need to use the limit() method to limit records in MongoDB. The method accepts one argument for the number type, which is the number of documents that you want to show.
Syntax
The basic syntax of limit() method is as follows −
>db.COLLECTION_NAME.find().limit(NUMBER)
Example
Consider that myycol collection has the following details.
{_id : ObjectId("507f191e810c19729de860e1"), title: "MongoDB Overview"}, {_id : ObjectId("507f191e810c19729de860e2"), title: "NoSQL Overview"}, {_id : ObjectId("507f191e810c19729de860e3"), title: "Phptpoint Overview"}
The following example displays only two documents when the document is being querying.
>db.mycol.find({},{"title":1,_id:0}).limit(2) {"title":"MongoDB Overview"} {"title":"NoSQL Overview"} >
If you do not specify the number argument in the limit() process, then all documents from the collection will be displayed.
In addition to the limit() method, there is one more skip() method that also accepts the argument for number type and is used to skip the number of documents.
Syntax
The basic syntax of skip() method is as follows −
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
Example
Following example will display only the second document.
>db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1) {"title":"NoSQL Overview"} >
Please note, the default value in skip() method is 0.