Skip to content

Commit 3f6e8dc

Browse files
johnnyshieldsp
andcommitted
MONGOID-5111 [Documentation Only] Add docs for limit/skip/batch_size (#5033)
* Fixes MONGOID-5111. - Documents limit, skip, and batch_size operators - Add warning to Queries + Persistence section about limit, etc being ignored. * minor fixes * Update queries.txt * Fix anchor for batch-size Co-authored-by: shields <[email protected]> Co-authored-by: Oleg Pudeyev <[email protected]>
1 parent 161abf3 commit 3f6e8dc

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

docs/tutorials/mongoid-queries.txt

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,69 @@ the default scope being evaluated first:
986986
embedded: false>
987987

988988

989+
Pagination
990+
==========
991+
992+
Mongoid provides the pagination operators ``limit``, ``skip``, and ``batch_size`` on ``Criteria``.
993+
994+
.. _limit:
995+
996+
``limit``
997+
---------
998+
999+
``limit`` sets the total number of documents to be returned by a query:
1000+
1001+
.. code-block:: ruby
1002+
1003+
Band.limit(5)
1004+
# =>
1005+
# #<Mongoid::Criteria
1006+
# selector: {}
1007+
# options: {:limit=>5}
1008+
# class: Band
1009+
# embedded: false>
1010+
1011+
.. _skip:
1012+
1013+
``skip``
1014+
--------
1015+
1016+
``skip`` (alias: ``offset``) sets the number of query results to skip
1017+
before returning documents. The ``limit`` value, if specified, will be applied
1018+
after documents are skipped. When performing pagination, ``skip`` is recommended
1019+
to be combined with :ref:`ordering <ordering>` to ensure consistent results.
1020+
1021+
.. code-block:: ruby
1022+
1023+
Band.skip(10)
1024+
# =>
1025+
# #<Mongoid::Criteria
1026+
# selector: {}
1027+
# options: {:skip=>10}
1028+
# class: Band
1029+
# embedded: false>
1030+
1031+
.. _batch-size:
1032+
1033+
``batch_size``
1034+
--------
1035+
1036+
When executing large queries, or when iterating over query results with an enumerator method such as
1037+
``Criteria#each``, Mongoid automatically uses the `MongoDB getMore command
1038+
<https://docs.mongodb.com/manual/reference/command/getMore/>`_ to load results in batches.
1039+
The default ``batch_size`` is 1000, however you may set it explicitly:
1040+
1041+
.. code-block:: ruby
1042+
1043+
Band.batch_size(500)
1044+
# =>
1045+
# #<Mongoid::Criteria
1046+
# selector: {}
1047+
# options: {:batch_size=>500}
1048+
# class: Band
1049+
# embedded: false>
1050+
1051+
9891052
.. _query-cache:
9901053

9911054
Query Cache
@@ -1580,6 +1643,11 @@ Mongoid supports persistence operations off of criteria
15801643
in a light capacity for when you want to expressively perform multi
15811644
document inserts, updates, and deletion.
15821645

1646+
.. warning::
1647+
1648+
Criteria ordering and pagination conditions, including ``order``, ``limit``,
1649+
``offset``, and ``batch_size``, will be ignored on the following operations.
1650+
15831651
.. list-table::
15841652
:header-rows: 1
15851653
:widths: 30 60

0 commit comments

Comments
 (0)