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

Model.bulkWrite updateOne.update and updateMany.update type UpdateFilter<TSchema> should be compatible with Model.updateOne and Model.updateMany type UpdateQuery<T> #11911

Closed
2 tasks done
ahmedelshenawy25 opened this issue Jun 7, 2022 · 1 comment · Fixed by #11962
Labels
typescript Types or Types-test related issue / Pull Request
Milestone

Comments

@ahmedelshenawy25
Copy link
Contributor

ahmedelshenawy25 commented Jun 7, 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.3.6

Node.js version

16.3.1

MongoDB server version

5.0.5

Description

Argument of type '{ updateOne: { filter: { name: string; }; update: UpdateQuery<IAnimal>; }; }[]' is not assignable to parameter of type 'AnyBulkWriteOperation<Document>[]'.
  Type '{ updateOne: { filter: { name: string; }; update: mongoose.UpdateQuery<IAnimal>; }; }' is not assignable to type 'AnyBulkWriteOperation<Document>'.
    Type '{ updateOne: { filter: { name: string; }; update: mongoose.UpdateQuery<IAnimal>; }; }' is not assignable to type '{ updateOne: UpdateOneModel<Document>; }'.
      The types of 'updateOne.update' are incompatible between these types.
        Type 'UpdateQuery<IAnimal>' is not assignable to type 'UpdateFilter<Document> | UpdateFilter<Document>[]'.
          Type 'UpdateQuery<IAnimal>' is missing the following properties from type 'UpdateFilter<Document>[]': length, pop, push, concat, and 29 more.ts(2345)

Steps to Reproduce

import mongoose, { UpdateQuery } from 'mongoose';
const { Schema, model } = mongoose;

async function run() {
  await mongoose.connect('mongodb://localhost:27017/test');

  interface IAnimal {
    name?: string;
  }

  const animalSchema = new Schema<IAnimal>({
    name: { type: String }
  });

  const Animal = model<IAnimal>('Animal', animalSchema);

  const writeOperations = ['foo', 'bar', 'baz'].map(name => {
    const changes: UpdateQuery<IAnimal> = {}

    if (name === 'foo') {
      changes.name = name.repeat(2);
    } else {
      changes.name = name.repeat(4);
    }

    return {
      updateOne: {
        filter: { name },
        update: changes
      }
    }
  })

  await Animal.bulkWrite(writeOperations);
}
run();
{
  "compilerOptions": {
    "target": "ES2020",
    "module": "ESNext",
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
  }
}

Expected Behavior

Model.bulkWrite (updateOne.update || updateMany.update) UpdateFilter<TSchema> and Model.updateOne UpdateQuery<T> should be compatible

@IslandRhythms IslandRhythms added the typescript Types or Types-test related issue / Pull Request label Jun 9, 2022
@vkarpov15 vkarpov15 added this to the 6.3.10 milestone Jun 17, 2022
vkarpov15 added a commit that referenced this issue Jun 21, 2022
fix(types): make Mongoose `UpdateQuery` compatible with MongoDB `UpdateFilter`
@ahmedelshenawy25

This comment was marked as spam.

@Automattic Automattic locked and limited conversation to collaborators Oct 25, 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

Successfully merging a pull request may close this issue.

3 participants