Skip to content

bulkSave doesn't throw an error even if one of the submitted documents was not updated  #14763

@ksyd9821

Description

@ksyd9821

Prerequisites

  • I have written a descriptive issue title

Mongoose version

8.x.x

Node.js version

20.11

MongoDB version

7.0

Operating system

None

Operating system version (i.e. 20.04, 11.3, 10)

No response

Issue

Hi all,

While troubleshooting we ran into some unexpected behavior of the bulkSave function.
If one of the submitted documents is outdated (has a lower version than the copy on the DB), this operation will not throw any error, even though the document won't be updated.
We expected it to behave similarly to the save function, which throws a VersionError in these cases.

Repro:

    public async foo() {
        const fooSchema = new mongoose.Schema({
            bar: { type: Number },
        });
        const model = mongoose.model("foo", fooSchema);
        await model.deleteMany();

        const foo = await model.create({
            bar: 0
        });

        // update 1
        foo.bar = 1;
        await foo.save();

        // parallel update
        const fooCopy = await model.findById(foo._id);
        fooCopy.bar = 99;
        await fooCopy.save();

        // update 2 - this should throw a version error
        foo.bar = 2;
        // await foo.save(); // throws a VersionError
        await model.bulkSave([foo]) // no error thrown, but update not applied

        // log result
        console.log(`bar: ${(await model.findById(foo._id)).bar}`); // logs 99
    }

Is this the expected behavior of bulkSave? Is there any possibility of making it fail if one of the documents has been already replaced by a newer version instead of returning silently?

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis issue is due to a mistake or omission in the mongoosejs.com documentationenhancementThis issue is a user-facing general improvement that doesn't fix a bug or add a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions