|
| 1 | +===================== |
| 2 | +Skip Returned Results |
| 3 | +===================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn how to skip a specified number of returned |
| 17 | +results from read operations. |
| 18 | + |
| 19 | +Sample Data |
| 20 | +~~~~~~~~~~~ |
| 21 | + |
| 22 | +Run the following snippet to load the documents into the ``ratings`` |
| 23 | +collection of the ``tea`` database: |
| 24 | + |
| 25 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/skip.go |
| 26 | + :language: go |
| 27 | + :dedent: |
| 28 | + :start-after: begin insertDocs |
| 29 | + :end-before: end insertDocs |
| 30 | + |
| 31 | +.. include:: /includes/fundamentals/tea-sample-data-ending.rst |
| 32 | + |
| 33 | +Skip |
| 34 | +---- |
| 35 | + |
| 36 | +To skip a specified number of returned results from a query, pass the |
| 37 | +number of documents you want to skip to the ``SetSkip()`` function of |
| 38 | +the read operation's options. |
| 39 | + |
| 40 | +Specify the options as the last parameter to the following read |
| 41 | +operation functions: |
| 42 | + |
| 43 | +- ``Find()`` |
| 44 | +- ``FindOne()`` |
| 45 | +- ``CountDocuments()`` |
| 46 | +- ``gridfs.Bucket.Find()`` |
| 47 | + |
| 48 | +If the number of documents exceeds the number of matched documents for a |
| 49 | +query, that query returns no documents. |
| 50 | + |
| 51 | +.. tip:: |
| 52 | + |
| 53 | + Passing in a negative number to the ``SetSkip()`` function results |
| 54 | + in a runtime error. |
| 55 | + |
| 56 | +Documents return in a natural order, which can lead to skipping random |
| 57 | +documents. To avoid this, use a ``SetSort()`` function before the |
| 58 | +``SetSkip()`` function. |
| 59 | + |
| 60 | +Example |
| 61 | +~~~~~~~ |
| 62 | + |
| 63 | +The following example performs the following actions in order with the |
| 64 | +``Find()`` function: |
| 65 | + |
| 66 | +- Sorts all the matched documents in ascending order |
| 67 | +- Skips the first two documents |
| 68 | + |
| 69 | +.. literalinclude:: /includes/fundamentals/code-snippets/CRUD/skip.go |
| 70 | + :language: go |
| 71 | + :dedent: |
| 72 | + :start-after: begin skip |
| 73 | + :end-before: end skip |
| 74 | + |
| 75 | +After running the preceding example, the output resembles the following: |
| 76 | + |
| 77 | +.. code-block:: none |
| 78 | + :copyable: false |
| 79 | + |
| 80 | + //results truncated |
| 81 | + [{_id ObjectID("...")} {type Oolong} {rating 7}] |
| 82 | + [{_id ObjectID("...")} {type Earl Grey} {rating 8}] |
| 83 | + [{_id ObjectID("...")} {type Masala} {rating 10}] |
| 84 | + |
| 85 | +.. tip:: Using Aggregation |
| 86 | + |
| 87 | + You can also include the :manual:`$skip </reference/operator/aggregation/skip/>` |
| 88 | + stage to specify a skip in an aggregation pipeline. |
| 89 | + |
| 90 | + The following example specifies the same sort and skip from the |
| 91 | + preceding example in an aggregation pipeline: |
| 92 | + |
| 93 | + .. literalinclude:: /includes/fundamentals/code-snippets/CRUD/skip.go |
| 94 | + :language: go |
| 95 | + :dedent: |
| 96 | + :start-after: begin aggregate skip |
| 97 | + :end-before: end aggregate skip |
| 98 | + |
| 99 | +Additional Information |
| 100 | +---------------------- |
| 101 | + |
| 102 | +For more information on performing read operations, see our guide on |
| 103 | +:doc:`retrieving data </fundamentals/crud/read-operations/retrieve>`. |
| 104 | + |
| 105 | +For information on specifying a sort, see our guide on :doc:`sorting |
| 106 | +results </fundamentals/crud/read-operations/sort>`. |
| 107 | + |
| 108 | +API Documentation |
| 109 | +~~~~~~~~~~~~~~~~~ |
| 110 | + |
| 111 | +For more information on any of the functions or types discussed in this |
| 112 | +guide, see the following API Documentation: |
| 113 | + |
| 114 | +- `Find() <{+api+}/mongo#Collection.Find>`__ |
| 115 | +- `FindOptions.SetSkip() <{+api+}/mongo/options#FindOptions.SetSkip>`__ |
| 116 | +- `Aggregate() <{+api+}/mongo#Collection.Aggregate>`__ |
| 117 | +- `CountDocuments() <{+api+}/mongo#Collection.CountDocuments>`__ |
| 118 | +- `gridfs.Bucket.Find() <{+api+}/mongo/gridfs#Bucket.Find>`__ |
0 commit comments