Prerequisites
Mongoose version
^9.6.2
Node.js version
20x
MongoDB server version
8x
Typescript version (if applicable)
5x
Description
Prerequisites
Versions
| Package |
Version |
| mongoose |
9.6.2 |
| typescript |
5.x |
| node.js |
20.x |
Description
$percentile is a valid MongoDB 8x accumulator operator but is missing from
Mongoose's AccumulatorOperator type in mongoose 9.6.2. $median works fine,
only $percentile throws a TypeScript compile error.
Steps to Reproduce
const pipeline: PipelineStage[] = [
{
$group: {
_id: null,
p95: {
$percentile: { // ❌ TS2353
input: '$orderTotal',
p: [0.95],
method: 'approximate',
},
},
},
},
];
await OrderModel.aggregate(pipeline);
Error
Object literal may only specify known properties,
and '$percentile' does not exist in type 'AccumulatorOperator'.ts(2353)
Steps to Reproduce
Steps to Reproduce
- Install mongoose
9.6.2 with TypeScript
- Create a typed aggregation pipeline using
PipelineStage[]
- Use
$percentile as an accumulator inside a $group stage
import { PipelineStage, Types } from 'mongoose';
const pipeline: PipelineStage[] = [
{
$match: {
vendor: new Types.ObjectId(),
status: { $ne: 'cancelled' },
createdAt: { $gte: new Date(), $lte: new Date() },
},
},
{
$project: {
_id: 0,
orderTotal: { $sum: '$items.total' },
},
},
{
$group: {
_id: null,
p95: {
$percentile: {
input: '$orderTotal',
p: [0.95],
method: 'approximate',
},
},
},
},
];
Note: $median works without any TypeScript error. Only $percentile is missing
from AccumulatorOperator type definition.
Expected Behavior
Expected Behavior
$percentile should be a valid key in AccumulatorOperator just like $median already is.
Workaround
p95: {
$percentile: {
input: '$orderTotal',
p: [0.95],
method: 'approximate',
},
},
References
Prerequisites
Mongoose version
^9.6.2
Node.js version
20x
MongoDB server version
8x
Typescript version (if applicable)
5x
Description
Prerequisites
Versions
Description
$percentileis a valid MongoDB 8x accumulator operator but is missing fromMongoose's
AccumulatorOperatortype in mongoose9.6.2.$medianworks fine,only
$percentilethrows a TypeScript compile error.Steps to Reproduce
Error
Object literal may only specify known properties,
and '$percentile' does not exist in type 'AccumulatorOperator'.ts(2353)
Steps to Reproduce
Steps to Reproduce
9.6.2with TypeScriptPipelineStage[]$percentileas an accumulator inside a$groupstageNote:
$medianworks without any TypeScript error. Only$percentileis missingfrom
AccumulatorOperatortype definition.Expected Behavior
Expected Behavior
$percentileshould be a valid key inAccumulatorOperatorjust like$medianalready is.Workaround
References