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

fix(types): support using UpdateQuery in bulkWrite() #12742

Merged
merged 1 commit into from Nov 30, 2022

Conversation

vkarpov15
Copy link
Collaborator

Fix #12595
Re: #11911

Summary

Not my favorite change, but unfortunately necessary if we want to support #12595 with extends Document. Once we drop support for extends Document, we can revisit this issue. See comments about why this change is necessary.

Examples

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();

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vkarpov15 vkarpov15 merged commit 440c9d6 into master Nov 30, 2022
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-12595-2 branch November 30, 2022 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants