Mongoose can’t find matching document with id when copying data from one collection to another.
I have a site where a user searches for a class in a Mongo DB collection called classes. Once it finds the class (which works), it should add a copy of that class to a Collection called classes_taken. However, I run into this error:
VersionError: No matching document found for id "64779936f186a42f6b09658b" version 0 modifiedPaths "_id, has_final, description, offered_fall, offered_spring, meets_with_subjects, instructors, joint_subjects, total_units, related_subjects, hass_attribute, pdf_option, is_half_class, level, url, subject_id, title, lab_units, design_units, public, offered_summer, lecture_units, preparation_units, is_variable_units, offered_IAP"
at generateVersionError (/Users/nayeemurrahman/Documents/Projects/extinguisher/server/node_modules/mongoose/lib/model.js:461:10)
at model.save (/Users/nayeemurrahman/Documents/Projects/extinguisher/server/node_modules/mongoose/lib/model.js:518:28)
at /Users/nayeemurrahman/Documents/Projects/extinguisher/server/index.js:44:20
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
version: 0,
modifiedPaths: [
'_id', 'has_final',
'description', 'offered_fall',
'offered_spring', 'meets_with_subjects',
'instructors', 'joint_subjects',
'total_units', 'related_subjects',
'hass_attribute', 'pdf_option',
'is_half_class', 'level',
'url', 'subject_id',
'title', 'lab_units',
'design_units', 'public',
'offered_summer', 'lecture_units',
'preparation_units', 'is_variable_units',
'offered_IAP'
]
This is my code for this process:
// POST request to add a class taken
app.post("/addClass", async (req, res) => {
// let's say a string was sent in the request body
// take that string and find the class in the database
// add that class to the database of classes taken
const subject_id = req.body.subject_id;
const classTaken = await ClassModel.findOne({
subject_id: subject_id,
});
// add this class to the database of classes taken
try {
const newClass = TakenClassesModel(classTaken);
// LINE BELOW IS WHERE ERROR STARTS
await newClass.save();
} catch (err) {
console.log(err);
}
res.json(classTaken);
});
How can I resolve this? Version does not matter to me at all. I simply want to take a whole data from classes and copy it to classes_taken.
2
Answers
I actually found a loop around that I don't understand why it works:
Simply do this: define a new variable that is an object with the same exact fields as the original. Then pass this to TakenClassModel.
I'm not entirely sure what difference this makes, but it's the only way I can seem to resolve the issue.
Here are a few possible reasons for this error:
the database.
the document in the database.
you are trying to update.
To resolve this issue, you can try the following steps:
trying to update and ensure they are correct.
the ID.
handle this case appropriately in your code and decide how to
proceed.
to fetch the latest version of the document and apply your updates
based on that.
It’s important to ensure that your code handles potential errors like this gracefully and provides appropriate feedback to the user or takes necessary actions based on the specific requirements of your application.