-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
bulkSave doesn't throw an error even if one of the submitted documents was not updated #14763
Comments
This issue is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 5 days |
As written, neither const mongoose = require('mongoose');
require('util').inspect.defaultOptions.depth = 10;
mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');
void async function main() {
const fooSchema = new mongoose.Schema({
bar: { type: Number },
}, { optimisticConcurrency: true });
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
}(); |
So long story short, there's no way for @AbdelrahmanHafez @hasezoey what do you think about this one? There's a couple of options:
|
@vkarpov15 yes, you are right. I forgot to mention that we have optimistic concurrency enabled by default on all schemas through a plugin. Not sure if this can help, but as a workaround we went with option 2 combined with a transaction. So if |
i dont fully know how
does this returns some kind of error in |
@hasezoey I'm a bit sad that none of the solutions are robust enough to provide the id(s) of the failing documents, and there's no way we can handle that from mongoose side. I'm leaning towards a combination of the second and third options. We make a note in the docs about this behavior, and have a global option/bulkSave option What do you think? |
I put in a PR for option (1) since that is the least brittle - in that case, we know all documents failed so we shouldn't update any Right now I'm thinking a combination of option (1) and option (2). In the option (1) case we know all documents failed, so we know we can handle that correctly. In the case where we have a mismatch, we don't know which documents were updated, so we don't know which documents to update or run middleware on, so we should throw an error. Likely a different class of error than I'm not necessarily a fan of the |
fix(model): throw error if `bulkSave()` did not insert or update any documents
Prerequisites
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 aVersionError
in these cases.Repro:
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?The text was updated successfully, but these errors were encountered: