@@ -17,32 +17,37 @@ Definition
17
17
18
18
.. versionadded:: 3.2
19
19
20
- Randomly selects the specified number of documents from its input.
20
+ Randomly selects the specified number of documents from the
21
+ input documents.
21
22
22
23
The :pipeline:`$sample` stage has the following syntax:
23
24
24
25
.. code-block:: javascript
25
26
26
- { $sample: { size: <positive integer> } }
27
+ { $sample: { size: <positive integer N> } }
28
+
29
+ ``N`` is the number of documents to randomly select.
27
30
28
31
Behavior
29
32
--------
30
33
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`:
34
42
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.
37
46
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::
41
48
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>`.
46
51
47
52
MMAPv1 May Return Duplicate Documents
48
53
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -86,5 +91,3 @@ The operation returns three random documents.
86
91
.. seealso::
87
92
88
93
:expression:`$rand (aggregation) <$rand>`
89
-
90
-
0 commit comments