Skip to content

Commit c2d28ac

Browse files
johnnyshieldsp
andauthored
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 687df5e commit c2d28ac

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

docs/reference/queries.txt

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

989989

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

9921055
Query Cache
@@ -1551,6 +1614,11 @@ Mongoid supports persistence operations off of criteria
15511614
in a light capacity for when you want to expressively perform multi
15521615
document inserts, updates, and deletion.
15531616

1617+
.. warning::
1618+
1619+
Criteria ordering and pagination conditions, including ``order``, ``limit``,
1620+
``offset``, and ``batch_size``, will be ignored on the following operations.
1621+
15541622
.. list-table::
15551623
:header-rows: 1
15561624
:widths: 30 60

0 commit comments

Comments
 (0)