Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect aggregation types for $group since 6.4.0 #12149

Closed
2 tasks done
Naeoth opened this issue Jul 25, 2022 · 2 comments
Closed
2 tasks done

Incorrect aggregation types for $group since 6.4.0 #12149

Naeoth opened this issue Jul 25, 2022 · 2 comments
Labels
typescript Types or Types-test related issue / Pull Request

Comments

@Naeoth
Copy link

Naeoth commented Jul 25, 2022

Prerequisites

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

Mongoose version

6.4.6

Node.js version

16.15.0

MongoDB server version

5.0

Description

The type changed on the 6.4.0: f57e007#diff-46be1d2c4ab7acfb5007f7bcccbf3d2e9551597d0bd0d91cc0400a4faffb76cfR116-R118

and gives an error on a valid mongoDB aggregation query.

error TS2769: No overload matches this call.
  Overload 1 of 2, '(pipeline?: PipelineStage[], options?: AggregateOptions, callback?: Callback<T[]>): Aggregate<T[]>', gave the following error.
    Type 'string' is not assignable to type 'Record<string | number | symbol, never>'.
  Overload 2 of 2, '(pipeline: PipelineStage[], callback?: Callback<T[]>): Aggregate<T[]>', gave the following error.
    Type 'string' is not assignable to type 'Record<string | number | symbol, never>'.

50         count: { $count: "$value" },

For a group the type should be as before:

$group: { _id: any } | { [key: string]: { [op in AccumulatorOperator]?: any } }

Steps to Reproduce

Model.aggregate([
    {
      $group: {
        _id: "$createdAt",
        count: { "$count": "$value" },
      },
    },
 ])

Expected Behavior

No response

@vkarpov15 vkarpov15 added this to the 6.4.8 milestone Jul 26, 2022
@vkarpov15 vkarpov15 added the typescript Types or Types-test related issue / Pull Request label Jul 26, 2022
@Naeoth
Copy link
Author

Naeoth commented Aug 4, 2022

@vkarpov15

I just test on 6.5.1, but your MR doesn't fix the bug :/

The Group interface is declared like that:

    export interface Group {
      /** [`$group` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/group) */
      $group: { _id: any } | { [key: string]: AccumulatorOperator }
    }

but the mongoDB documention describes:

    {
      $group:
        {
          _id: <expression>, // Group key
          <field1>: { <accumulator1> : <expression1> },
          ...
        }
     }

So mongoose should have something like:

   export interface Group {
     /** [`$group` reference](https://docs.mongodb.com/manual/reference/operator/aggregation/group) */
     $group: { _id: any } | { [key: string]: { [op in AccumulatorOperator]?: any } } }
   }

@vkarpov15 vkarpov15 reopened this Aug 9, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.5.1, 6.5.3 Aug 9, 2022
@vkarpov15
Copy link
Collaborator

@Naeoth I took a closer look and the typings as written are correct, and do support the syntax described in the MongoDB documentation. { [key: string]: AccumulatorOperator } is more correct because MongoDB doesn't support multiple accumulator expressions on one field. Multiple accumulators throw "The field 'field-name-here' must specify one accumulator"

@vkarpov15 vkarpov15 removed this from the 6.5.3 milestone Aug 11, 2022
@Automattic Automattic locked as resolved and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
typescript Types or Types-test related issue / Pull Request
Projects
None yet
Development

No branches or pull requests

2 participants