May 17, 2021 MongoDB
If you need to read a specified number of data records in MongoDB, you can use MongoDB's Limit method, the limit() method, to accept a numeric parameter that specifies the number of records read from MongoDB.
The basic syntax of the limit() method is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER)
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 following example shows 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 parameters in the limit() method, all the data in the collection is displayed.
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 amount of data, and the skip method also accepts a numeric parameter as the number of record bars skipped.
The skip() method script syntax format is as follows:
>db.COLLECTION_NAME.find().limit(NUMBER).skip(NUMBER)
The above example shows only the second document data
>db.mycol.find({},{"title":1,_id:0}).limit(1).skip(1) {"title":"NoSQL Overview"} >
Note: The default parameter for the skip() method is 0.