- Reference >
- Operators >
- Aggregation Pipeline Operators >
- Pipeline Aggregation Stages >
- $sample (aggregation)
$sample (aggregation)¶
On this page
Definition¶
Behavior¶
In order to get N random documents:
- If N is greater than or equal to 5% of the total documents in the
collection,
$sample
performs a collection scan, performs a sort, and then select the top N documents. As such, the$sample
stage is subject to the sort memory restrictions. - If N is less than 5% of the total documents in the collection,
- If using WiredTiger Storage Engine,
$sample
uses a pseudo-random cursor over the collection to sample N documents. - If using MMAPv1 Storage Engine,
$sample
uses the_id
index to randomly select N documents.
- If using WiredTiger Storage Engine,
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.