YouTip LogoYouTip

Mongodb Limit Skip

MongoDB Limit and Skip Methods\n\n

MongoDB Limit and Skip Methods

\n
\n\n

MongoDB Limit() Method

\n

If you need to read a specific number of data records in MongoDB, you can use the MongoDB limit() method. If you want to skip a specific number of documents and read data, you can use the skip() method.

\n

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 or batch processing on large datasets.

\n\n

limit() Method

\n

The limit() method is used to limit the number of documents returned by a query.

\n\n

Syntax

\n

The basic syntax of the limit() method is as follows:

\n
db.collection.find().limit(<limit>)
\n
    \n
  • <limit>: The number of documents to return.
  • \n
\n\n

Example

\n
// Return the first 10 documents\ndb.myCollection.find().limit(10);
\n\n

Example

\n

The data in the collection col is as follows:

\n
{ "_id" : ObjectId("56066542ade2f21f36b0313a"), "title" : "PHP Tutorial", "description" : "PHP is a powerful server-side scripting language for creating dynamic and interactive websites.", "by" : "Tutorial", "url" : "", "tags" : , "likes" : 200 }\n{ "_id" : ObjectId("56066549ade2f21f36b0313b"), "title" : "Java Tutorial", "description" : "Java is a high-level programming language introduced by Sun Microsystems in May 1995.", "by" : "Tutorial", "url" : "", "tags" : , "likes" : 150 }\n{ "_id" : ObjectId("5606654fade2f21f36b0313c"), "title" : "MongoDB Tutorial", "description" : "MongoDB is a NoSQL database.", "by" : "Tutorial", "url" : "", "tags" : , "likes" : 100 }
\n

The following example displays two records from the query results:

\n
> db.col.find({},{"title":1,_id:0}).limit(2)\n{ "title" : "PHP Tutorial" }\n{ "title" : "Java Tutorial" }\n>
\n

Note: If you do not specify a parameter in the limit() method, all data in the collection will be displayed.

\n\n
\n\n

skip() Method

\n

In addition to using the limit() method to read a specific number of records, you can also use the skip() method to skip a specified number of records. The skip() method also accepts a numeric parameter as the number of records to skip.

\n

The skip() method is used to skip a specified number of documents, thereby implementing pagination or batch queries.

\n\n

Syntax

\n

The syntax format of the skip() method is as follows:

\n
db.collection.find().skip(<skip>)
\n
    \n
  • <skip>: The number of documents to skip.
  • \n
\n\n

Example

\n
// Skip the first 10 documents and return the next 10 documents\ndb.myCollection.find().skip(10).limit(10);
\n\n

Example

\n

The following example will only display the second document:

\n
>db.col.find({},{"title":1,_id:0}).limit(1).skip(1)\n{ "title" : "Java Tutorial" }\n>
\n

Paginated query:

\n\n

Example

\n
// First page, 10 documents per page\ndb.myCollection.find().skip(0).limit(10);\n\n// Second page, 10 documents per page\ndb.myCollection.find().skip(10).limit(10);\n\n// Third page, 10 documents per page\ndb.myCollection.find().skip(20).limit(10);
\n

Note: The default parameter for the skip() method is 0.

\n\n

Important Notes

\n
    \n
  • The skip() and limit() methods are typically used together to implement pagination. However, using skip() on large datasets can lead to performance issues because MongoDB needs to scan and skip a specified number of documents when executing the query. Therefore, it is recommended to use the skip() method only when necessary and to avoid using it consecutively on large datasets.
  • \n
  • When combining skip() and limit(), skip() should be used before limit() to avoid unexpected behavior.
  • \n
\n

By using the limit() and skip() methods, you can implement pagination for query results in MongoDB, improving query flexibility and performance.

\n\n
\n\n

User Notes

\n
    \n
  1. \n

    #0 HeavenlyBeing_2008

    \n

    yan***n_xi@163.com

    \n
    db.col.find({},{"title":1,_id:0}).limit(2)
    \n

    Supplementary explanation:

    \n
      \n
    • The first {} holds the WHERE condition; if empty, it returns all documents in the collection.
    • \n
    • The second {} specifies which fields to display or hide (0 means hide, 1 means show).
    • \n
    \n

    9 years ago (2017-04-17)

    \n
  2. \n
  3. \n

    #0 joyran

    \n

    xin***iyihan@126.com

    \n

    To read 100 records after the 10th record, equivalent to limit (10,100) in SQL.

    \n
    > db.COLLECTION_NAME.find().skip(10).limit(100)
    \n

    The above example skips the first 10 records in the collection and returns 100 records.

    \n

    Combining skip and limit can achieve pagination.

    \n

    9 years ago (2017-04-20)

    \n
  4. \n
  5. \n

    #0 dragon

    \n

    lib***uan_87@126.com

    \n

    Reference

    \n

    Supplementary explanation: The skip and limit methods are only suitable for pagination of small data volumes. For millions of records, efficiency will be very low because the skip method counts records one by one. It is recommended to use where_limit.

    \n

    After reviewing some materials, I found that all sources say the same thing:

    \n

    Do not easily use Skip for queries, otherwise, performance will drop sharply when the data volume is large. This is because Skip counts records one by one, and it naturally becomes slow when there are many.

    \n

    So, should Skip be avoided? How to avoid it? First, let's review the timestamp-based pagination scheme in SQL. This method uses the ordered nature of fields and queries to fetch data, which can directly avoid a large amount of counting. That is, if such conditions can be included, query efficiency will improve. Is this really the case? Let's verify:

    \n

    Here we assume we are querying the 100,001st record, and the Amount value of this record is: 2399927. Let's write two statements as follows:

    \n
    db.test.sort({"amount":1}).skip(100000).limit(10) //183ms\ndb.test.find({amount:{$gt:2399927}}).sort({"amount":1}).limit(10) //53ms
    \n

    The results are already included in the comments. Obviously, the performance of the latter is one-third of the former, and the gap is very large. This also confirms the theory that Skip has poor efficiency.

    \n

    You can contact via email libaoyuan_87@126.com to learn from each other.

    \n

    9 years ago (2017-09-14)

    \n
  6. \n
  7. \n

    #0 niuyongjie

    \n

    niu***gjie@163.com

    \n

    limit(n) is used to specify the number of records to display, while skip(n) is used to skip a number of records from the first record among those that meet the conditions. These two functions can be used interchangeably.

    \n

    For example: find({},{age:1,_id:0}).limit(2).skip(1), among the documents that meet the conditions, two documents are to be displayed, and the display position starts after skipping the first record. This is not very easy to understand.

    \n

    If written as find({},{age:1,_id:0}).skip(1).limit(2), among the documents that meet the conditions, first skip the first document, then display two documents. This is easier to understand.

    \n

    9 years ago (2017-11-26)

    \n
  8. \n
  9. \n

    #0 doc.z

    \n

    bin***real@163.com

    \n

    It should be noted that the execution order of skip, sort, and limit here is independent of their position. However, when used in aggregation, they have the characteristics of a pipeline stream, and the execution order is sequential according to their position.

    \n

    8 years ago (2018-10-26)

    \n
  10. \n
  11. \n

    #0 MelodyPocket

    \n

    106***4379@qq.com

    \n
  12. \n
← Mongodb SortJava Properties Class β†’