|
| 1 | +================== |
| 2 | +cursor.batchSize() |
| 3 | +================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. method:: cursor.batchSize() |
| 8 | + |
| 9 | + The :method:`batchSize() <cursor.batchSize()>` method specifies the |
| 10 | + number of documents the cursor returns per batch. Specifying the |
| 11 | + batchSize is a low-level optimization whose effects are not visible |
| 12 | + to the user. |
| 13 | + |
| 14 | + The :method:`batchSize() <cursor.batchSize()>` method takes the |
| 15 | + following parameter: |
| 16 | + |
| 17 | + :param size: |
| 18 | + |
| 19 | + The number of documents to return per batch. The ``size`` |
| 20 | + also affects whether to close the cursor after the first |
| 21 | + batch. Refer to the specific driver ``batchSize`` |
| 22 | + documentation for the cursor behavior as determined by the |
| 23 | + ``size``. |
| 24 | + |
| 25 | + .. note:: |
| 26 | + |
| 27 | + The actual number of documents in the batch may be smaller as |
| 28 | + determined by the limit or the 4 megabyte size limit of the |
| 29 | + server reply. |
| 30 | + |
| 31 | + Consider the following examples of the :method:`batchSize() |
| 32 | + <cursor.batchSize()>` method in the :program:`mongo` shell: |
| 33 | + |
| 34 | + - Set the number of documents returned by the :method:`find() |
| 35 | + <db.collection.find()>` method to ``10`` per each batch: |
| 36 | + |
| 37 | + .. code-block:: javascript |
| 38 | + |
| 39 | + var curs = db.animals.find().batchSize(10) |
| 40 | + |
| 41 | + The cursor remains open so that you can iterate through the cursor |
| 42 | + ``curs`` to access all documents in the ``animals`` collection. |
| 43 | + |
| 44 | + - Set the number of documents returned by the :method:`find() |
| 45 | + <db.collection.find()>` method to ``10`` per each batch and close |
| 46 | + after the first batch: |
| 47 | + |
| 48 | + .. code-block:: javascript |
| 49 | + |
| 50 | + var curs = db.animals.find().batchSize(-10) |
| 51 | + |
| 52 | + In the :program:`mongo` shell, the cursor closes after the first |
| 53 | + batch so that you can iterate the cursor only ``10`` times. |
0 commit comments