Skip to content

DOCS-14855 reword-sample (#6012) #6015

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

Merged
merged 1 commit into from
Oct 18, 2021
Merged
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
33 changes: 19 additions & 14 deletions source/reference/operator/aggregation/sample.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,37 @@ Definition

.. versionadded:: 3.2

Randomly selects the specified number of documents from its input.
Randomly selects the specified number of documents from the
input documents.

The :pipeline:`$sample` stage has the following syntax:

.. code-block:: javascript

{ $sample: { size: <positive integer> } }
{ $sample: { size: <positive integer N> } }

``N`` is the number of documents to randomly select.

Behavior
--------

: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 all of the following conditions are true, :pipeline:`$sample` uses a
pseudo-random cursor to select the ``N`` documents:

- :pipeline:`$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 any of the previous conditions are false, :pipeline:`$sample`:

If all the following conditions are met, ``$sample`` uses a
pseudo-random cursor to select documents:
- Reads all documents that are output from a preceding aggregation
stage or a collection scan.
- Performs a random sort to select ``N`` documents.

- ``$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
.. note::

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>`.
Random sorts are subject to the :ref:`sort memory restrictions
<sort-memory-limit>`.

MMAPv1 May Return Duplicate Documents
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down