The $rand operator generates a random floating-point number between 0 and 1 for each document in an aggregation pipeline. Use it for sampling, shuffling, A/B testing, and any workflow that needs probabilistic logic.
01
Random [0, 1)
One value per document.
02
Empty Syntax
{ $rand: {} }
03
Sampling
Filter by threshold.
04
Shuffle
Sort on random field.
05
Use Cases
Tests, lotteries, A/B.
06
vs $sample
Flexible vs fixed count.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $rand operator returns a pseudo-random double in the range [0, 1) — that is, greater than or equal to 0 and less than 1. Each document evaluated in the pipeline receives its own independent random value.
Unlike stages that pick a fixed number of documents (such as $sample), $rand gives you a numeric value you can compare, sort, scale, or store. This makes it ideal for percentage-based sampling and custom random logic.
💡
Beginner Tip
$rand takes no arguments — always write { $rand: {} }. Use it inside $project or $addFields to add a random field, or inside $match with $expr to filter probabilistically.
Foundation
📝 Syntax
The $rand operator takes an empty object (no parameters):
mongosh
{ $rand: {} }
Syntax Rules
No arguments — the object must be empty: {}.
Output range — [0, 1): includes 0, excludes 1.
Per document — each document gets a new random value when evaluated.
Use inside $project, $addFields, $set, or $match with $expr.
Available in MongoDB 4.4.1 and later.
Not a query filter on its own — wrap in an expression stage.
$rand gives [0, 1). Multiplying by 100 gives [0, 100), and $floor truncates to integers 0–99. Change the multiplier for different ranges.
Applications
🚀 Use Cases
Random sampling — select a percentage of documents for analysis or QA.
A/B testing — assign users to experiment groups with $cond and $rand.
Shuffling — randomize document order before $limit for fair picks.
Test data — generate random scores, weights, or lottery numbers in pipelines.
🧠 How $rand Works
1
Pipeline reaches $rand
When a stage evaluates { $rand: {} }, MongoDB invokes its random number generator for each document.
Input
2
A random double is produced
Each document receives a value in [0, 1) — independent of other documents in the same evaluation.
Generate
3
The value is used or stored
Store it in a field, compare it in $match, sort on it, or scale it with $multiply.
Output
=
🎲
Probabilistic pipelines
Sample, shuffle, split, or score documents with built-in randomness.
Wrap Up
Conclusion
The $rand operator generates random numbers in [0, 1) inside MongoDB aggregation pipelines. With no arguments and simple syntax, it powers sampling, shuffling, A/B tests, and custom probabilistic filters.
Remember each run produces different values, and percentage sampling is approximate. Next in the series: $range.
Compare to a threshold in $expr for percentage sampling
Pair with $cond for A/B group assignment
Use $sample when you need an exact document count
Scale with $multiply and $floor for integer ranges
❌ Don’t
Pass arguments to $rand (it accepts none)
Expect identical results on every pipeline run
Assume percentage samples are exact counts
Use $rand as a top-level query filter without $expr
Rely on it for cryptographic security (it is pseudo-random)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $rand
Use these points when adding randomness to aggregation pipelines.
5
Core concepts
🎲01
Random [0, 1)
Per document.
Purpose
📝02
Empty Object
{ $rand: {} }
Syntax
🛠03
% Sampling
$lt threshold.
Pattern
🔀04
Shuffle
Sort on random field.
Use case
⚠05
Non-deterministic
Changes each run.
Edge case
❓ Frequently Asked Questions
$rand generates a random floating-point number between 0 (inclusive) and 1 (exclusive) for each document in an aggregation pipeline. Use it for sampling, shuffling, test data, and probabilistic logic.
The syntax is { $rand: {} }. It takes an empty object and accepts no arguments. Use it inside stages like $project, $addFields, or $match with $expr.
$rand returns a value in the range [0, 1) — greater than or equal to 0 and less than 1. Each document gets its own independent random value.
Use $match with $expr: { $match: { $expr: { $lt: [ { $rand: {} }, 0.2 ] } } } } to keep roughly 20% of documents. Adjust the threshold (0.2) for your desired sample rate.
$rand adds a random number per document for flexible filtering or sorting. The $sample stage randomly selects a fixed number of documents from the pipeline. Use $sample when you need an exact count; use $rand for percentage-based or custom random logic.