$percentile (Percentile Accumulator)
Last updated: 2026-10-09
$percentile (Percentile Accumulator)
$percentile is an accumulator returning values at given percentiles (0 to 100) of a field within a group, available since MongoDB 7.0. The p argument accepts a single value or an array. method supports approximate (default, faster) and exact. Common for p95/p99 latency and salary quantiles.
| Category | Accumulators |
|---|---|
| MongoDB Version | 7.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $group: { _id: <key>, pct: { $percentile: { input: "$field", p: [ <percentile> ], method: "approximate" } } } }
Returns:Values at the requested percentiles (array in, array out)
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $percentile: p50, p95, p99 latency per service
db.latency.aggregate([
{ $group: { _id: '$service', pct: { $percentile: { input: '$ms', p: [50, 95, 99], method: 'approximate' } } } }
])
Output:
{ "_id": "api", "pct": [12, 45, 80] }
📌 Notes
- Requires MongoDB 7.0 or later
- p must be between 0 and 100
- Non-numeric values in the input are ignored
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QHow do I compute p95?
APass p: [95] to get that quantile, commonly used for latency monitoring.
QHow accurate is approximate?
AErrors are small on large datasets and it is fast — fine for monitoring; use exact when precision matters.
QWhat is the difference between $percentile and $median?
A$median is fixed at the 50th percentile; $percentile covers any percentile.