- Reference >
- Operators >
- Aggregation Pipeline Stages >
- $sample (aggregation)
$sample (aggregation)¶
On this page
Definition¶
Behavior¶
$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 the following conditions are met, $sample
uses a
pseudo-random cursor to select 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
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 $sample
stage is subject to the
sort memory restrictions.
Warning
$sample
may output the same document more than once in
its result set. For more information, see Cursor Isolation.
Example¶
Given a collection named users
with the following documents:
The following aggregation operation randomly selects 3
documents from the
collection:
The operation returns three random documents.