MongoDB - Write Limit

Limit() Method
To limit records in MongoDB, you need to use the limit() method . The method takes one numeric type argument, which is the number of documents you want to display.
Syntax
The basic syntax of the limit() method is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER)
example
Consider a collection myycol that has the following data.
{ "_id" : ObjectId ( 5983548781331adf45ec5 ), "title" : "MongoDB Overview" } { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title " : "Tutorials Point Overview" }
The following example will only show two documents when requesting a document.
> db . mycol . find ({},{ "title" : 1 , _id : 0 }). limit ( 2 ) { "title" : "MongoDB Overview" } { "title" : "NoSQL Overview" } >
If you don't provide a number argument in the limit() method, it will display all documents from the collection.
MongoDB Skip() method
Besides the limit() method, there is another skip() method that also takes a numeric type argument and is used to skip the number of documents.
Syntax
The basic syntax of the skip() method is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
example
In the following example, only the second document will be displayed.
> db . mycol . find ({},{ "title" : 1 , _id : 0 }). limit ( 1 ). skip ( 1 ) { "title" : "NoSQL Overview" } >
Note that the default value in the skip() method is 0.