Skip to content

QueryFilter is not assignable to filter part of AnyBulkWriteOperation #15910

@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

Similar issue to #15909, the QueryFilter type is not assignable to the updateOne.filter part of AnyBulkWriteOperation. This could be me using the type incorrectly since I get different results depending on how I use them:

Works

import { model, Schema } from 'mongoose';

import type { AnyBulkWriteOperation, QueryFilter, Types } from 'mongoose';

interface FooType {
  _id: Types.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() },
};

const result = await FooModel.bulkWrite([
  {
    updateOne: {
      filter: query,
      update: {
        $set: {
          date: new Date(),
        },
      },
    },
  },
]);

Doesn't work

import { model, Schema } from 'mongoose';

import type { AnyBulkWriteOperation, QueryFilter, Types } from 'mongoose';

interface FooType {
  _id: Types.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() },
};

const test: AnyBulkWriteOperation<FooType>[] = [
  {
    updateOne: {
      filter: query, // Error here
      update: {
        $set: {
          date: new Date(),
        },
      },
    },
  },
];

const result = await FooModel.bulkWrite(test);
Type '_QueryFilter<{ _id: ObjectId; date: Date; "_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 ......' is not assignable to type 'Filter<FooType>'.
  Type '_QueryFilter<{ _id: ObjectId; date: Date; "_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 ......' is not assignable to type '{ date?: Condition<Date> | undefined; _id?: Condition<ObjectId> | undefined; }'.
    Types of property 'date' are incompatible.
      Type 'Condition<ApplyBasicQueryCasting<Date>> | undefined' is not assignable to type 'Condition<Date> | undefined'.
        Type 'null' is not assignable to type 'Condition<Date> | undefined'.ts(2322)

Expected Behavior

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    typescriptTypes or Types-test related issue / Pull Request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions