skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.

    const classTakenS = {
      _id: classTaken._id,
      rating: classTaken.rating,
      title: classTaken.title,
    };
    
    const newClass = TakenClassesModel(classTakenS);
    await newClass.save();
    

    I'm not entirely sure what difference this makes, but it's the only way I can seem to resolve the issue.


  2. Here are a few possible reasons for this error:

    1. The document with the specified ID and version has been deleted from
      the database.
    2. The version number provided does not match the current version of
      the document in the database.
    3. The document has been modified by another process since the version
      you are trying to update.

    To resolve this issue, you can try the following steps:

    1. Double-check the ID and version number of the document you are
      trying to update and ensure they are correct.
    2. Verify that the document exists in the database by querying it using
      the ID.
    3. If the document has been deleted or does not exist, you may need to
      handle this case appropriately in your code and decide how to
      proceed.
    4. If the document has been modified by another process, you may need
      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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search