mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

Aggregation pipeline typing broke after update cannot match by _id in $match stage.

Open steadexe opened this issue 4 years ago • 2 comments

Prerequisites

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

Mongoose version

6.4.0

Node.js version

16.15.1

MongoDB server version

5.0.3

Description

When I try to perform an aggregation pipeline with a $match stage using an ObjectId, it does work but it triggers a typing error.

TS2769: No overload matches this call.   Overload 1 of 2, '(pipeline?: PipelineStage[], options?: AggregateOptions, callback?: Callback<any[]>): Aggregate<any[]>', gave the following error.     Type 'ObjectId' is not assignable to type 'Expression'.   Overload 2 of 2, '(pipeline: PipelineStage[], callback?: Callback<any[]>): Aggregate<any[]>', gave the following error.     Type 'ObjectId' is not assignable to type 'Expression'.

If I remove the cast, it does not work, but typing error goes away. I didn't had any issue before updating to 6.4.0.

Thanks for helping :)

Steps to Reproduce

const id = '62b45aad4d042f1a4ed455b4';

await myModel.aggregate([
      {
        $match: {
          _id: Types.ObjectId.createFromHexString(id)
        }
      }
    ]);

Expected Behavior

No response

steadexe avatar Jun 27 '22 13:06 steadexe

I think this is already fixed at https://github.com/Automattic/mongoose/pull/11969, but it isn't published yet.

jyeros avatar Jun 27 '22 13:06 jyeros

6.4.1 fixed the issue for me.

jyeros avatar Jun 28 '22 11:06 jyeros