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 when reading 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 usually used together to implement paginated queries or batch processing on large datasets.
limit() method
The limit() method is used to limit the number of documents returned by a query result.
Syntax
The basic syntax of the limit() method is as follows:
db.collection.find().limit()
: The number of documents to return.
Example
// Return the first 10 documents
db.
myCollection
.
find
(
)
.
limit
(
10
)
;
Example
The data in the col collection is as follows:
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP 教程", "description" : "PHP 是一种创建动态交互性站点的强有力的服务器端脚本语言。", "by" : "Runoob Tutorial", "url" : "http://www.runoob.com", "tags" : [ "php" ], "likes" : 200 }
{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java 教程", "description" : "Java 是由Sun Microsystems公司于1995年5月推出的高级程序设计语言。", "by" : "Runoob Tutorial", "url" : "http://www.runoob.com", "tags" : [ "java" ], "likes" : 150 }
{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "Runoob Tutorial", "url" : "http://www.runoob.com", "tags" : [ "mongodb" ], "likes" : 100 }
The following example displays two records from the query result:
> db.col.find({},{"title":1,_id:0}).limit(2)
{ "title" : "PHP 教程" }
{ "title" : "Java 教程" }
>
**Note:** If you do not specify the parameter in the limit() method, all data in the collection is displayed.
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 amount of data; 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 implementing pagination or batch queries.
Syntax
The syntax of the skip() method is as follows:
db.collection.find().skip()
: The number of documents to skip.
Example
// Skip the first 10 documents and return the next 10 documents
db.
myCollection
.
find
(
)
.
skip
(
10
)
.
limit
(
10
)
;
Example
The following example displays only the second document
>db.col.find({},{"title":1,_id:0}).limit(1).skip(1)
{ "title" : "Java 教程" }
>
Paginated query:
Example
// Page 1, 10 documents per page
db.
myCollection
.
find
(
)
.
skip
(
0
)
.
limit
(
10
)
;
// Page 2, 10 documents per page
db.
myCollection
.
find
(
)
.
skip
(
10
)
.
limit
(
10
)
;
// Page 3, 10 documents per page
db.
myCollection
.
find
(
)
.
skip
(
20
)
.
limit
(
10
)
;
**Note:** The default parameter of the skip() method is 0.
Notes
skip()andlimit()methods are usually used together to implement paginated queries. However, using them on large datasets canskip()cause performance issues, because MongoDB needs to scan and skip a specified number of documents when executing a query. Therefore, it is recommended to use them only when needed;skip()and try to avoid using them consecutively on large datasets.- When combined with
skip()andlimit(), theskip()should belimit()before, to avoid unexpected behavior.
By using limit() and skip() methods, you can paginate query results in MongoDB, improving the flexibility and performance of queries.