Skip to content

DOCS-9744: $sample can only use a random cursor if it is the first st… #2823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions source/reference/operator/aggregation/sample.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,21 @@ Definition
Behavior
--------

In order to get N random documents:
:pipeline:`$sample` uses one of two methods to obtain N random
documents, depending on the size of the collection, the size of N,
and ``$sample``'s position in the pipeline.

- If N is greater than or equal to 5% of the total documents in the
collection, :pipeline:`$sample` performs a collection scan, performs
a sort, and then select the top N documents. As such, the
:pipeline:`$sample` stage is subject to the :ref:`sort memory
restrictions <sort-memory-limit>`.
If all the following conditions are met, ``$sample`` uses a
pseudo-random cursor to select documents:

- If N is less than 5% of the total documents in the collection,
- ``$sample`` is the first stage of the pipeline
- N is less than 5% of the total documents in the collection
- The collection contains more than 100 documents

- If using :doc:`/core/wiredtiger`, :pipeline:`$sample` uses a
pseudo-random cursor over the collection to sample N documents.

- If using :doc:`/core/mmapv1`, :pipeline:`$sample` uses the ``_id``
index to randomly select N documents.
If any of the above conditions are NOT met, ``$sample`` performs a
collection scan followed by a random sort to select N documents. In
this case, the :pipeline:`$sample` stage is subject to the
:ref:`sort memory restrictions <sort-memory-limit>`.

.. warning::

Expand Down