Skip to content

$match type in aggregate() doesn't take QueryFilter for the model as type #15909

@richardsimko

Description

@richardsimko

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.0.1

Node.js version

N/A

MongoDB server version

N/A

Typescript version (if applicable)

5.0.3

Description

If passing a QueryFilter to the $match type a type error is produced. This is a regression between 9.0.0 and 9.0.1.

Steps to Reproduce

interface FooType {
  _id: ObjectId;
  date: Date;
}
const fooSchema = new Schema<FooType>({
  date: { type: Date, required: true },
});

const FooModel = model<FooType>('foo', fooSchema);

const query: QueryFilter<FooType> = {
  date: { $lte: new Date() },
};

await FooModel.aggregate([{ $match: query }]); // No overload matches this call
No overload matches this call.
  Overload 1 of 2, '(pipeline?: PipelineStage[] | undefined, options?: AggregateOptions | undefined): Aggregate<any[]>', gave the following error.
    Type '_QueryFilter<{ _id: ObjectId; "_id.default": (val: any) => any; "_id.get": (fn: Function) => ObjectId; "_id.schema": Schema<any, Model<any, any, any, any, any, any, any>, ... 8 more ..., {} & ... 1 more ... & { ...; }> | undefined; ... 23 more ...; "_id.validateAll": (validators: (RegExp | ... 1 more ... | Validator...' is not assignable to type '_QueryFilter<Record<string, any>>'.
      Type '_QueryFilter<{ _id: ObjectId; "_id.default": (val: any) => any; "_id.get": (fn: Function) => ObjectId; "_id.schema": Schema<any, Model<any, any, any, any, any, any, any>, ... 8 more ..., {} & ... 1 more ... & { ...; }> | undefined; ... 23 more ...; "_id.validateAll": (validators: (RegExp | ... 1 more ... | Validator...' is not assignable to type 'RootFilterOperators<{ [x: string]: any; }>'.
        Types of property '$and' are incompatible.
          Type 'Filter<{ _id?: ApplyBasicQueryCasting<ObjectId> | undefined; "_id.default"?: ApplyBasicQueryCasting<(val: any) => any> | undefined; ... 25 more ...; "_id.validateAll"?: ApplyBasicQueryCasting<...> | undefined; }>[] | undefined' is not assignable to type 'Filter<{ [x: string]: any; }>[] | undefined'.
            Type 'Filter<{ _id?: ApplyBasicQueryCasting<ObjectId> | undefined; "_id.default"?: ApplyBasicQueryCasting<(val: any) => any> | undefined; ... 25 more ...; "_id.validateAll"?: ApplyBasicQueryCasting<...> | undefined; }>[]' is not assignable to type 'Filter<{ [x: string]: any; }>[]'.
              Type 'Filter<{ _id?: ApplyBasicQueryCasting<ObjectId> | undefined; "_id.default"?: ApplyBasicQueryCasting<(val: any) => any> | undefined; ... 25 more ...; "_id.validateAll"?: ApplyBasicQueryCasting<...> | undefined; }>' is not assignable to type 'Filter<{ [x: string]: any; }>'.
                Type 'Filter<{ _id?: ApplyBasicQueryCasting<ObjectId> | undefined; "_id.default"?: ApplyBasicQueryCasting<(val: any) => any> | undefined; ... 25 more ...; "_id.validateAll"?: ApplyBasicQueryCasting<...> | undefined; }>' is not assignable to type '{ [x: string]: any; _id?: Condition<ObjectId> | undefined; }'.
                  Types of property '_id' are incompatible.
                    Type 'Condition<ObjectId | ObjectId[] | null> | undefined' is not assignable to type 'Condition<ObjectId> | undefined'.
                      Type 'null' is not assignable to type 'Condition<ObjectId> | undefined'.
  Overload 2 of 2, '(pipeline: PipelineStage[]): Aggregate<any[]>', gave the following error.
    Type '_QueryFilter<{ _id: ObjectId; "_id.default": (val: any) => any; "_id.get": (fn: Function) => ObjectId; "_id.schema": Schema<any, Model<any, any, any, any, any, any, any>, ... 8 more ..., {} & ... 1 more ... & { ...; }> | undefined; ... 23 more ...; "_id.validateAll": (validators: (RegExp | ... 1 more ... | Validator...' is not assignable to type '_QueryFilter<Record<string, any>>'.
      Type '_QueryFilter<{ _id: ObjectId; "_id.default": (val: any) => any; "_id.get": (fn: Function) => ObjectId; "_id.schema": Schema<any, Model<any, any, any, any, any, any, any>, ... 8 more ..., {} & ... 1 more ... & { ...; }> | undefined; ... 23 more ...; "_id.validateAll": (validators: (RegExp | ... 1 more ... | Validator...' is not assignable to type 'RootFilterOperators<{ [x: string]: any; }>'.
        Types of property '$and' are incompatible.
          Type 'Filter<{ _id?: ApplyBasicQueryCasting<ObjectId> | undefined; "_id.default"?: ApplyBasicQueryCasting<(val: any) => any> | undefined; ... 25 more ...; "_id.validateAll"?: ApplyBasicQueryCasting<...> | undefined; }>[] | undefined' is not assignable to type 'Filter<{ [x: string]: any; }>[] | undefined'.
            Type 'Filter<{ _id?: ApplyBasicQueryCasting<ObjectId> | undefined; "_id.default"?: ApplyBasicQueryCasting<(val: any) => any> | undefined; ... 25 more ...; "_id.validateAll"?: ApplyBasicQueryCasting<...> | undefined; }>[]' is not assignable to type 'Filter<{ [x: string]: any; }>[]'.
              Type 'Filter<{ _id?: ApplyBasicQueryCasting<ObjectId> | undefined; "_id.default"?: ApplyBasicQueryCasting<(val: any) => any> | undefined; ... 25 more ...; "_id.validateAll"?: ApplyBasicQueryCasting<...> | undefined; }>' is not assignable to type 'Filter<{ [x: string]: any; }>'.
                Type 'Filter<{ _id?: ApplyBasicQueryCasting<ObjectId> | undefined; "_id.default"?: ApplyBasicQueryCasting<(val: any) => any> | undefined; ... 25 more ...; "_id.validateAll"?: ApplyBasicQueryCasting<...> | undefined; }>' is not assignable to type '{ [x: string]: any; _id?: Condition<ObjectId> | undefined; }'.
                  Types of property '_id' are incompatible.
                    Type 'Condition<ObjectId | ObjectId[] | null> | undefined' is not assignable to type 'Condition<ObjectId> | undefined'.
                      Type 'null' is not assignable to type 'Condition<ObjectId> | undefined'.ts(2769)

Expected Behavior

No type errors, the query's type is valid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    helpThis issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions