“VersionError: No matching document found” error on Node.js/Mongoose

后端 未结 5 1422
攒了一身酷
攒了一身酷 2020-12-13 17:34

I\'m relatively new to Node.js and Mongo/Mongoose, and I\'m having a very difficult time troubleshooting a specific Mongoose error:

VersionError: No

相关标签:
5条回答
  • 2020-12-13 18:13

    I had this issue because I was removing an item from an array of a Document using splice function.

    I fixed replacing splice function with pull function from mongoose.

    0 讨论(0)
  • 2020-12-13 18:14

    I had the same error when i tried to update a user's reference IDs to an email. The fix was really simple with async / await! Here the code snippet, hope it helps.

    email
        .save()
        .then(() =>
          User.findById(email.from).then(async sender => { // declare function as async
            sender.emails.sent.push(email._id);
            await sender.save(); // wait for save() to complete before proceeding
          }).catch((err) => console.log(err))
        )
        .then(() =>
          User.findById(email.to).then(async receiver => { // same as above
            receiver.emails.received.push(email._id);
            await receiver.save(); // same as above
          }).catch((err) => console.log(err))
        )
        .then(() => res.status(200).json({ message: successMessage }))
        .catch(err => console.log(err));
    
    0 讨论(0)
  • 2020-12-13 18:16

    Per request, here is an outline of our problem, and how we resolved it:

    In our system we created a custom Document locking routine (using redis-lock), wherein the following happened in this precise (incorrect) order:

    INCORRECT ORDER OF OPERATIONS:

    1. Client request received
    2. Document locked
    3. Document retrieved
    4. Document edited
    5. Document unlocked
    6. Client request resolved
    7. Document saved

    Once you see it written out, the problem is obvious: we were saving our Documents outside our Document lock.

    Let's assume #6 takes 100ms in our system. That is a 100ms window wherein if any other requests grabs that same Document, we're going to have a save conflict (the titled error in this Question is basically a save conflict IMHO).

    In other words/example: in our system, Request A grabbed Version 1 of Document X, edited it, then unlocked it, but before Request A saved the Document, Request B grabbed Document X and incremented it to Version 2 (read up on Mongo versions for more info about this). Then Request A resolves its Client request and goes to save Document X, but it's trying to save Version 1, and now it sees it has Version 2, and thus the error above.

    So the fix is easy. Save your Documents inside your lock. (In the above example, move #7 to before #5. See below.)

    CORRECT/FIXED ORDER OF OPERATIONS

    1. Client request received
    2. Document locked
    3. Document retrieved
    4. Document edited
    5. Document saved
    6. Document unlocked
    7. Client request resolved

    (You could make an argument that #6 and #7 should be swapped, but that is outside the scope of Mongo/Mongoose/this question.)

    I am going to leave this question un-answered for a while and see if anyone can shed some light on a better way to isolate the relevant code and troubleshoot this issue. In our case, this was a very systemic problem and VERY challenging to troubleshoot for our skill level at the time.

    0 讨论(0)
  • 2020-12-13 18:19

    It propably points to saving the same document concurrently as robertklep points out.

    We had a similar issue running concurrent saves on the same document using async.parallel.

    0 讨论(0)
  • 2020-12-13 18:24

    This error can also occur when your process maintains an outdated version of a document in memory and then tries to save it at some point after it has been updated by another process.

    0 讨论(0)
提交回复
热议问题