Skip to content Skip to sidebar Skip to footer

Mongoose - Version Error: No Matching Document Found For Id

Context: I have a Post Mongoose model that contains a csv_files array field to store csv strings. I make a fetch API request from a different web app to POST the csv strings for a

Solution 1:

When saving an object to Mongo DB you have to understand that Mongo DB has a version control system in place. This helps ensure that if you save an object once, when saving it again you don't end up overwriting the previously saved data.

This is the error you're seeing. If you want to force the object to update regardless of version control in this particular instance you may want to use .update() instead. This will force the object to be updated regardless of its currently saved state.

This is because .save() watches and cares about version controls, while .update() will update the object regardless of version control.


Solution 2:

I deleted version property from requests for update at body.

delete req.body.__v;


Solution 3:

While it would appear a .save() is the right approach here, an .update() command would get the job done while ignoring "race conditions" that cause this error to occur. Mongo DB is throwing this error because it is concerned that I am saving an older version of the document that has already been updated:

v1 is sent to client v1 is saved, and updated to v2 in Mongo DB v1 is trying to be saved again, but Mongo DB already has v2 stored, error is thrown A better approach is to send v1 to the client and when the cart object changes, synchronize the object with the new cart object no matter what. This can be done via .update() rather than through .save().

This is because .save() watches and cares about version controls, while .update() will update the object regardless of version control.


Solution 4:

Looks like @robertklep was right. I fixed my issue by wrapping the first API call in a Promise and then executing the second API call after the first one completed.

I'll wait to mark this as the answer just in case anyone comes across a better solution.


Solution 5:

just to comment here what my problem apparently was.

Error Message:

message: 'No matching document found for id "5c86d3a15bbcb965d85d80d1" version 3', name: 'VersionError', version: 3

My code:

update: function (data) {

        var session = new Session(data);
        database.logging("INFO", "session-update", session.cid, session.order.orderNumber);
        return new Promise(function (resolve, reject) {

            session.save()
                .then(savedSession => resolve(savedSession))
                .catch(savedSessionError => reject(savedSessionError))
        });

    }

My parameter 'data' was already a Mongoose object and I was creating a new one and trying to save() it. So like @dacopenhagen said, save() checks the version of the document and it's throwing an VersionError error.

How I fixed it Just removing the creation of the Session() object. Removed this line

var session = new Session(data);

I hope this help.


Post a Comment for "Mongoose - Version Error: No Matching Document Found For Id"