MongoDB Limit () method
If you need to read a specified number of data records in MongoDB, you can use the MongoDB limit method, and the limit () method accepts a numeric parameter that specifies the number of record bars read from MongoDB.
Grammar
The basic syntax for the limit () method is as follows:
>db. Collection_name.find (). Limit (number)
Instance
The data in the collection Myycol is as follows:
{"_id": ObjectId (5983548781331ADF45EC5), "title": "MongoDB Overview"} {"_id": ObjectId (5983548781331ADF45EC6), "title": "NoSQL Overview"} {"_id": ObjectId (5983548781331ADF45EC7), "title": "Tutorials Point Overview"}
The above example shows the two records in the query document:
>db.mycol.find ({},{"title": 1,_id:0}). Limit (2) {"title": "MongoDB Overview"} {"title": "NoSQL Overview"}>
Note: If you do not specify a parameter in the limit () method, all data in the collection is displayed.
MongoDB Skip () method
In addition to using the limit () method to read a specified amount of data, we can also use the Skip () method to skip a specified number of data, and the Skip method also accepts a numeric parameter as the number of skipped records.
Grammar
The Skip () method script syntax is formatted as follows:
>db. Collection_name.find (). Limit (number). Skip (number)
Instance
The above example will only display the second document data
>db.mycol.find ({},{"title": 1,_id:0}). Limit (1). Skip (1) {"title": "NoSQL Overview"}>
Note:The default parameter of the Skip () method is 0.
MongoDB Limit and Skip methods