Skip to content

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

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
10 changes: 10 additions & 0 deletions source/reference/operator/aggregation/sample.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ In order to get N random documents:
- If using :doc:`/core/mmapv1`, :pipeline:`$sample` uses the ``_id``
index to randomly select N documents.

:pipeline:`$sample` can use a pseudo-random cursor to find documents
if:

- It is the first stage in the aggregation pipeline
- N is less than 5% of the total documents in the collection
- The collection contains over 100 documents

Otherwise :pipeline:`$sample` performs a random sort on the
incoming documents and selects the first N documents.

.. warning::

:pipeline:`$sample` may output the same document more than once in
Expand Down