$sampleRate
Emits each document with the given probability. Useful for lightweight sampling or throttling without shuffling the pipeline.
Syntax
{ "$sampleRate": <rate> }
{ "$sampleRate": { "rate": <rate>, "seed": <number> } }
ratemust be between0and1. A document is kept when a random draw is less than or equal to the rate.seed(optional) makes the sampling deterministic, which is handy for tests.
Example
Input
[
{ "eventId": 1 },
{ "eventId": 2 },
{ "eventId": 3 },
{ "eventId": 4 }
]
Stage
{ "$sampleRate": { "rate": 0.5, "seed": 42 } }
Output
[
{ "eventId": 2 },
{ "eventId": 3 },
{ "eventId": 4 }
]
With the provided seed, events 2, 3, and 4 are retained. Without a seed, the sample varies across executions.
Tips
- Apply
$matchor$projectbefore sampling to reduce payload size. - Use after
$sortif you need sampling from a specific ordering of documents.