MongoDB Limit and Skip Methods
MongoDB Limit() Method
If you need to read a specified number of data records in MongoDB, you can use MongoDB's limit() method. If you want to skip a specified number of documents and read data, you can use the skip() method.
The limit() method is used to limit the number of documents returned by a query, while the skip() method is used to skip a specified number of documents. These two methods are often used together to implement pagination queries or batch processing on large datasets.
limit() Method
The limit() method is used to limit the number of documents returned by a query.
Syntax
The basic syntax of the limit() method is as follows:
db.collection.find().limit(<limit>)
<limit>: The number of documents to return.
Example
db.myCollection.find().limit(10);
Example
The data in the collection col is as follows:
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "Example", "url" : "http://www.example.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "Example", "url" : "http://www.example.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "Example", "url" : "http://www.example.com", "tags" : [ "mongodb" ], "likes" : 100 }
The following example displays two records from the query documents:
> db.col.find({},{"title":1,_id:0}).limit(2)
{ "title" : "PHP 教程" }
{ "title" : "Java 教程" }
>
Note:If you do not specify a parameter in the limit() method, all data in the collection will be displayed.
skip() Method
In addition to using the limit() method to read a specified number of data records, we can also use the skip() method to skip a specified number of data records. The skip() method also accepts a numeric parameter as the number of records to skip.
The skip() method is used to skip a specified number of documents, thereby enabling pagination or batch queries.
Syntax
The syntax format of the skip() method is as follows:
db.collection.find().skip(<skip>)
<skip>: The number of documents to skip.
Example
db.myCollection.find().skip(10).limit(10);
Example
The following example will only display the second document data
>db.col.find({},{"title":1,_id:0}).limit(1).skip(1)
{ "title" : "Java 教程" }
>
Pagination query:
Example
db.myCollection.find().skip(0).limit(10);
// Second page, 10 documents per page
db.myCollection.find().skip(10).limit(10);
// Third page, 10 documents per page
db.myCollection.find().skip(20).limit(10);
Note:The default parameter of the skip() method is 0.
Notes
skip()andlimit()The methods are usually used together to implement pagination queries. However, using them on large datasetsskip()may cause performance issues, because MongoDB needs to scan and skip the specified number of documents when executing the query, so it is recommended to use them only when necessaryskip()method, and try to avoid continuous use on large datasets.- When combined
skip()andlimit(),skip()should belimit()used before, to avoid unexpected behavior.
By usinglimit()andskip()method, you can implement pagination of query results in MongoDB, improving query flexibility and performance.