$rand is a MongoDB aggregation expression that returns a random floating-point number in the half-open range [0, 1)—at least 0, and always less than 1. Each document gets its own value.
Remember
$rand = random float in [0, 1)
{ $rand: {} }
no arguments — always empty {}
per document → independent value
not stable across runs
~20% sample:
$lt: [ { $rand: {} }, 0.2 ]
exact count → use $sample stage instead
For percentage sampling, compare with $lt inside $match + $expr.
Piece
Required?
Role
{}
Yes
Empty object — no options
Output
—
Float in [0, 1)
$lt + rate
No
Keep ~N% of documents
Pseudocode
Pseudocode
for each document:
r = random_float() // 0 ≤ r < 1
// store
emit { ..., score: r }
// or keep ~20%
if r < 0.2:
keep document
else:
skip
Reference
Cheat Sheet
Keep these facts handy while adding randomness to pipelines.
Topic
Detail
Type
Aggregation expression (not a find filter alone)
Syntax
{ $rand: {} }
Range
[0, 1)
Args
None — object must be empty
Stability
Different every run
~20% sample
$lt: [ { $rand: {} }, 0.2 ]
vs $sample
Rate vs exact count
Try it
Live Preview
Generate a fresh [0, 1) value (like $rand), and optionally scale it to a dice roll 1–6.
Simulates { $rand: {} }. Each click draws a new number.
Result
$rand → (click Run for a new value)
range: 0 ≤ r < 1
Trace
Worked Walkthrough — Random Ticket Score
Three support tickets sit in a queue. We give each a random score in [0, 1), then keep only tickets with score < 0.2 (about 20%).
Calculation
Documents:
A, B, C
Each gets:
score = $rand → e.g. 0.41, 0.07, 0.88
(your numbers will differ)
Keep if score < 0.2:
A 0.41 → skip
B 0.07 → keep
C 0.88 → skip
About 1 in 5 docs kept on average.
Code
MongoDB Examples
We use three tickets. First insert them, then attach a random score, then keep roughly 20% with $match. Run these in mongosh. Your random numbers will differ each run.
Example 1 — Sample Data
Imagine three support tickets: Login, Billing, and Shipping. We save them in a collection named tickets.
[
{ _id: 2, topic: "Billing" }
]
// Sometimes 0 docs, sometimes 2–3.
// Average keep rate ≈ 20%.
How It Works
Draw again.$match evaluates a new $rand per document.
Compare to 0.2.$lt keeps the doc only when the draw is below the rate.
Expect variation. With only three docs, a single run may keep 0–3 tickets.
Edge Cases & Pitfalls
Check these before relying on $rand in production pipelines.
args
Empty object only
Write { $rand: {} }. There is no seed argument and no min/max options on the operator itself.
stable
Not reproducible
Each run can differ. Do not use $rand for stable ids, caching keys, or audits that must match yesterday’s output.
count
Need exact N?
Percentage filters with $rand are approximate. Use the $sample stage for a fixed document count.
scale
Integers need math
For 1–6 style rolls, combine $multiply, $floor, and $add. $rand alone is always a float in [0, 1).
Analysis
Performance Notes
Approach
Cost
Best for
$rand + $addFields
Cheap per doc
Scores / shuffle keys
$match + rate
Scan + filter
~percentage samples
$sample stage
Optimized pick
Exact N documents
Drawing a float is cheap, but filtering a huge collection still reads many docs. Prefer $sample when you only need a fixed-size random subset.
Remember
Key Takeaways
Role: random float in [0, 1) per document.
Syntax: always { $rand: {} }.
Sampling:$lt vs a rate for ~percent; $sample for exact N.
Caution: results change every run.
One line:$rand answers “give this document a fresh number in [0, 1).”
Frequently Asked Questions
$rand returns a random floating-point number greater than or equal to 0 and less than 1 for each document. Use it in aggregation for sampling, shuffling, test fields, and probabilistic filters.
Use { $rand: {} }. It takes an empty object and accepts no arguments. Place it inside stages such as $addFields, $project, or $match with $expr.
The range is [0, 1)—inclusive of 0, exclusive of 1. Each document gets its own independent random value.
Use $match with $expr: { $match: { $expr: { $lt: [ { $rand: {} }, 0.2 ] } } }. Change 0.2 to any rate between 0 and 1.
$rand gives a random number per document for custom logic. The $sample stage picks a fixed count of documents. Prefer $sample for an exact size; prefer $rand for percentage-based or custom random rules.
No. Each pipeline run (and each evaluation) can produce different numbers. Do not rely on $rand for stable ids or reproducible reports.