Skip to content

TypeScript: $percentile missing from AccumulatorOperator in $group stage #16288

Description

@jsdev-robin

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

^9.6.2

Node.js version

20x

MongoDB server version

8x

Typescript version (if applicable)

5x

Description

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

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

  1. Install mongoose 9.6.2 with TypeScript
  2. Create a typed aggregation pipeline using PipelineStage[]
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions