I am getting this version error in MongoDB due to the save() method. I did some research about it and got to know that the save method uses versioning. To skip it we can use the update method, but by using update, I am getting another error "Performing an update on the path '_id' would modify the immutable field '_id".
Is there any other way to solve this version error?
code example:
let fileDoc = await File.findById(req.params.id);
fileDoc.history.unshift({
user: req.user._id,
status: status,
createdAt: new Date()
});
if (is_false_reject) {
fileDoc.isFalseReject = is_false_reject;
fileDoc.falseRejectedAt = new Date();
}
fileDoc.status = status;
fileDoc.comment = comment;
await fileDoc.save();
Error by save() method: VersionError: No matching document found for id "e6a83b9b-42be-41b7-87ed-e21eff052c61.pdf" version 2 modifiedPaths "history, status" at generateVersionError (/home/node/app/node_modules/mongoose/lib/model.js:439:10) at model.Model.save (/home/node/app/node_modules/mongoose/lib/model.js:495:28) at /home/node/app/helpers/file.js:525:23 at runMicrotasks () at processTicksAndRejections (node:internal/process/task_queues:94:5) { version: 2, modifiedPaths: [ 'history', 'status' ]
Please look on https://mongoosejs.com/docs/guide.html#versionKey
We can disable the version key in mongoose but that is not a best practice, Please review the documentation.
If you face issue after this solution please comment, I will try my best to solve your issue