@@ -17,32 +17,37 @@ Definition
1717
1818 .. versionadded:: 3.2
1919
20- Randomly selects the specified number of documents from its input.
20+ Randomly selects the specified number of documents from the
21+ input documents.
2122
2223 The :pipeline:`$sample` stage has the following syntax:
2324
2425 .. code-block:: javascript
2526
26- { $sample: { size: <positive integer> } }
27+ { $sample: { size: <positive integer N> } }
28+
29+ ``N`` is the number of documents to randomly select.
2730
2831Behavior
2932--------
3033
31- :pipeline:`$sample` uses one of two methods to obtain N random
32- documents, depending on the size of the collection, the size of N,
33- and ``$sample``'s position in the pipeline.
34+ If all of the following conditions are true, :pipeline:`$sample` uses a
35+ pseudo-random cursor to select the ``N`` documents:
36+
37+ - :pipeline:`$sample` is the first stage of the pipeline.
38+ - ``N`` is less than 5% of the total documents in the collection.
39+ - The collection contains more than 100 documents.
40+
41+ If any of the previous conditions are false, :pipeline:`$sample`:
3442
35- If all the following conditions are met, ``$sample`` uses a
36- pseudo-random cursor to select documents:
43+ - Reads all documents that are output from a preceding aggregation
44+ stage or a collection scan.
45+ - Performs a random sort to select ``N`` documents.
3746
38- - ``$sample`` is the first stage of the pipeline
39- - N is less than 5% of the total documents in the collection
40- - The collection contains more than 100 documents
47+ .. note::
4148
42- If any of the above conditions are NOT met, ``$sample`` performs a
43- collection scan followed by a random sort to select N documents. In
44- this case, the :pipeline:`$sample` stage is subject to the
45- :ref:`sort memory restrictions <sort-memory-limit>`.
49+ Random sorts are subject to the :ref:`sort memory restrictions
50+ <sort-memory-limit>`.
4651
4752MMAPv1 May Return Duplicate Documents
4853~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -86,5 +91,3 @@ The operation returns three random documents.
8691.. seealso::
8792
8893 :expression:`$rand (aggregation) <$rand>`
89-
90-
0 commit comments