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

Typings: New Aggregation Type Issues [DateExpressionOperatorReturningNumber, AddToSet, ...] #12017

Closed
2 tasks done
MichaelFedora opened this issue Jun 29, 2022 · 4 comments
Closed
2 tasks done
Milestone

Comments

@MichaelFedora
Copy link

MichaelFedora commented Jun 29, 2022

Prerequisites

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

Running:

  • Mongoose 6.4.1
  • Typescript 4.7.4
  • Node 14.19.1

Issue

This new update has messed up a log of my aggregation typings (like others).

1. Add DateExpressionOperatorReturningNumber to NumberExpression

This doesn't work currently, but it should. I believe it was forgotten:

const a: Expression = { $subtract: [
  { $dayOfWeek: new Date() } as Expression.DayOfWeek, // errors
  2
] };

2. Some stages and operators specify "Expressions" when they can take values as well

This breaks quite a few things ($addToSet as well):

const a: Expression.Reduce = {
  $reduce: {
    input: '$values',
    initialValue: { depth: -1 }, // errors
    in: {
      depth: '$$this.depth' // errors
    }
  }
};

const b: Expression.Reduce = {
  $reduce: {
    input: '$values',
    initialValue: 0, // errors
    in: { $add: ['$$value', '$$this'] }
  }
};

const c: PipelineStage.Set = {
  $set: {
    child: {
      foo: 'bar' // errors
    },
    friend: new ObjectId() // errors
  }
};

const a: Expression.ToInt = { $toInt: 2.5 }; // errors

3. AddToSet requires ArrayExpression

According to the docs it just takes an "expression" ( https://www.mongodb.com/docs/manual/reference/operator/aggregation/addToSet/ ).

4. Cond doesn't allow nested Conds

...because it is too strict on the first requirement being a BooleanExpression. I think switching most operators to accept a ConditionalExpressionOperator (with a <T> or otherwise) would help a lot, because IfNull is also throwing things.


Otherwise, reintroducing the old Pipeline type and/or having a more flexible Pipeline type in parallel, if we wanted/needed less strict checks would be great.

@MichaelFedora MichaelFedora changed the title Types: Add DateExpressionOperatorReturningNumber to NumberExpression Typings: New Aggregation Type Issues [DateExpressionOperatorReturningNumber, AddToSet, ...] Jun 29, 2022
@Davies-Owen
Copy link

I'm experiencing this as well. Another example: $add and $subtract don't allow nested Conds:

const d: Expression.Add = {
  $add: [
    '$value',
    {
      $cond: { // errors
        if: {
          $lte: ["$value", "$otherValue"],
        },
        then: 12,
        else: 0,
      },
    },
  ],
};

@skrtheboss
Copy link
Contributor

Avg requires ArrayExpression
According to the docs it just takes an "expression" or array of "expression" https://www.mongodb.com/docs/manual/reference/operator/aggregation/avg/#syntax

const d: Expression.Avg = { $avg: { $subtract: [{ $ifNull: ['$end', new Date()] }, '$start'] } }; // errors

@vkarpov15 vkarpov15 added this to the 6.4.3 milestone Jul 2, 2022
@billouboq
Copy link
Contributor

One of the type that does not work well is $ifNull inside other expression, had issues with it :

error TS2322: Type '{ $add: { $ifNull: (string | number)[]; }[]; }' is not assignable to type 'number | Expression | Expression[]'.
  Types of property '$add' are incompatible.
    Type '{ $ifNull: (string | number)[]; }[]' is not assignable to type '(string | number | NativeDate | ArrayElemAt | First | Last | Reduce | ObjectToArray | SetDifference | ... 78 more ... | Convert<...>)[]'.
      Type '{ $ifNull: (string | number)[]; }' is not assignable to type 'string | number | NativeDate | ArrayElemAt | First | Last | Reduce | ObjectToArray | SetDifference | ... 78 more ... | Convert<...>'.
        Object literal may only specify known properties, and '$ifNull' does not exist in type 'NativeDate | ArrayElemAt | First | Last | Reduce | ObjectToArray | SetDifference | SetIntersection | ... 77 more ... | Convert<...>'.

385        { $ifNull: ['$metrics.shares', 0] },

@darwin403
Copy link

Facing the same issue $ifNull problem.

No overload matches this call.
  Overload 1 of 2, '(pipeline?: PipelineStage[] | undefined, options?: AggregateOptions | undefined, callback?: Callback<{ date: string; totalVisibility: number; avgPosition: number; }[]> | undefined): Aggregate<...>', gave the following error.
    Type '{ $ifNull: (string | number)[]; }' is not assignable to type 'ArrayExpression<any>'.
      Object literal may only specify known properties, and '$ifNull' does not exist in type 'any[] | ArrayElemAt | First | Last | Reduce | ConcatArrays | Filter | Map | ObjectToArray | Range | ... 12 more ... | Shift'.
  Overload 2 of 2, '(pipeline: PipelineStage[], callback?: Callback<{ date: string; totalVisibility: number; avgPosition: number; }[]> | undefined): Aggregate<{ date: string; totalVisibility: number; avgPosition: number; }[]>', gave the following error.
    Type '{ $ifNull: (string | number)[]; }' is not assignable to type 'ArrayExpression<any>'.
      Object literal may only specify known properties, and '$ifNull' does not exist in type 'any[] | ArrayElemAt | First | Last | Reduce | ConcatArrays | Filter | Map | ObjectToArray | Range | ... 12 more ... | Shift'.

75             avgPosition: { $avg: { $ifNull: ["$position", 100] } },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants