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

Unordered bulkWrite() with validation errors doesn't execute the bulk write #13176

Closed
2 tasks done
vkarpov15 opened this issue Mar 15, 2023 · 2 comments
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@vkarpov15
Copy link
Collaborator

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

Node.js version

Doesn't matter

MongoDB server version

Doesn't matter

Typescript version (if applicable)

No response

Description

General idea is the following code throws a validation error and doesn't execute the one valid bulk write op, even though ordered: false is set

    const userSchema = new Schema({
      num: Number
    });
    const User = db.model('User', userSchema);


    await User.bulkWrite([{
      updateOne: {
        filter: { },
        update: { num: 'not a number' },
        upsert: true
      },
      {
        updateOne: { filter: {}, update: { num: 42 } }
      }
    }], { ordered: false }).then(() => null, err => err);

Reported in Slack: https://mongoosejsteam.slack.com/archives/C2CA1AWQP/p1678894562556919

"Hey I’m trying to use writeBulk with multiple updateOne unordered. When one entity throws error (For example doesn’t pass scheme validation) all the bulk get reverts, and I get a general error of cast fails. I would like to update the entities that able to be updated, and get error that contains the entities failed to update, similar to the the insertMany behavior. Does anyone have an idea?"

Steps to Reproduce

See above

Expected Behavior

No response

@vkarpov15 vkarpov15 added this to the 6.10.5 milestone Mar 15, 2023
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Mar 15, 2023
@IslandRhythms IslandRhythms added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 17, 2023
@IslandRhythms
Copy link
Collaborator

Remove the .then() to see the error. Report is correct, no entries in the db after bulkWrite runs.

const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
  num: Number
});

const User = mongoose.model('User', testSchema);

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

  await User.bulkWrite([{ updateOne: { filter: {}, update: { num: 'not a number'}, upsert: true }},
    {
      updateOne: { filter: {}, update: { num: 42 } }
    }], {ordered: false}).then(() => null, err => err);

    console.log(await User.find())

    console.log('done');
}

run();

vkarpov15 added a commit that referenced this issue Mar 27, 2023
vkarpov15 added a commit that referenced this issue Apr 6, 2023
fix(model): execute valid write operations if calling `bulkWrite()` with ordered: false
@doomhz
Copy link

doomhz commented Nov 23, 2023

Unfortunately, this fix broke the bulkWrite() function for UNordered ops (options = { ordered: false }). The bulkWrite function never returns when ops = [], since the check for if (ops.length === 0) { return cb(null, getDefaultBulkwriteResult()); } was moved inside the new if (ordered) { block.

A potential solution would be to move the empty ops array check above the new if block and return early from the callback function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants