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
DocumentNotFoundError instead of VersionError during save() #10974
Comments
So I am sort of able to reproduce this issue. It seems to be more of an issue where the document is not yet in the the model when the save is called. Can you show that the document is in the database when the error is being thrown? |
I am sure that the document is in the database, as before and after there are successful calls with the same document id:
Maybe one key hint is that I am running the save() and actually just writing the same data, so that no updateOne is triggered by the change detection. I only see these
|
After rereading your issue, It appears your initial assumption about the cause of the problem is correct.
That is the only way i am able to get it to pop up exactly as stated. The script const mongoose = require('mongoose');
mongoose.set('debug', true)
const robotSchema = new mongoose.Schema({
name: String
}, {optimisticConcurrency: true });
const Robot = new mongoose.model('Robot', robotSchema);
async function run() {
await mongoose.connect('mongodb://localhost:27017/', {useNewUrlParser: true,
useUnifiedTopology: true,});
await mongoose.connection.dropDatabase();
const entry = await Robot.create({name: 'WallE'});
const changes = await Robot.findOne({_id: entry._id}).orFail().exec();
const other = await Robot.findOne({_id: entry._id}).orFail().exec();
changes.name = 'John';
await changes.save();
other.name = 'WallE';
await other.save();
}
run(); |
Thanks for your quick responses! |
This occurs when the underlying document that Mongoose is trying to save isn't found, likely because the document was deleted. In v6.0.14 that will throw a VersionError rather than a DocumentNotFoundError. |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Model.save()
can throwDocumentNotFoundError
although the document exists and only differs in the expected__v
. According to my understanding this should yield aVersionError
instead. When enabling debug mode, I see one weird query that seems to trigger theDocumentNotFoundError
:However, in the resulting exception,
__v
is not mentioned:Usually, I'd just expect an
updateOne
but nofindOne
call during.save()
.If the current behavior is a bug, please provide the steps to reproduce.
The error is thrown sporadically. To trigger the error I believe a competing modification to the document is needed so that there is a version mismatch when saving.
What is the expected behavior?
robot.save()
should either succeed or throw a VersionError if a competing modification took place. However, sometimes I see a DocumentNotFoundError instead, although the document exists and subsequent queries for the same document succeed.What are the versions of Node.js, Mongoose and MongoDB you are using?
NodeJS: 16.13.0
Mongoose: 6.0.12
MongoDB: 4.4.10
The text was updated successfully, but these errors were encountered: