Mongoose: Increasing the number of versions of my documents does not work, and I get a version error when I try to save

When I try to save my document, I get a VersionError: No matching document found error similar to this SO question.

After reading this blog post, it looks like the problem is with the version of my document. That I was messing around with the array, so I need to update the version.

However, calling document.save() does not work for me. When I exit a document before and after calling save() , document._v is one and the same.

I also tried making document._v = document._v++ , which also didn't work.


code

 exports.update = function(req, res) { if (req.body._id) { delete req.body._id; } User.findById(req.params.id, function(err, user) { if (err) return handleError(res, err); if (!user) return res.send(404); var updated = _.extend(user, req.body); // doesn't increment the version number. causes problems with saving. see http://aaronheckmann.blogspot.com/2012/06/mongoose-v3-part-1-versioning.html console.log('pre increment: ', updated); updated.increment(); // updated._v = updated._v++; console.log('post increment: ', updated); updated.save(function(err) { if (err) return handleError(res, err); return res.json(200, user); }); }); }; 

Output

 pre increment: { _id: 5550baae1b571aafa52f070c, provider: 'local', name: 'Adam', email: ' azerner3@gmail.com ', hashedPassword: '/vahOqXwCwKQKtcV3KBQeFge/YB0xtqOj+YDyck7gzyALA/IP7u7BfqQhlVHBQT26//XfBTkaOCK2bQXg65OzA==', salt: 'MvzXW7D4xuyGQBJNeFRoUg==', __v: 32, drafts: [], starredSkims: [], skimsCreated: [ 5550cfdab8dcacd1a7892aa4 ], role: 'user' } post increment: { _id: 5550baae1b571aafa52f070c, provider: 'local', name: 'Adam', email: ' azerner3@gmail.com ', hashedPassword: '/vahOqXwCwKQKtcV3KBQeFge/YB0xtqOj+YDyck7gzyALA/IP7u7BfqQhlVHBQT26//XfBTkaOCK2bQXg65OzA==', salt: 'MvzXW7D4xuyGQBJNeFRoUg==', __v: 32, drafts: [], starredSkims: [], skimsCreated: [ 5550cfdab8dcacd1a7892aa4 ], role: 'user' } 
+5
source share
2 answers

The problem here is with using __v and trying to update manually. .increment does not actually increment immediately, but it sets an internal flag for the model that processes the increment. I cannot find the documentation for .increment , so I assume that it is probably used internally. The problem is related to trying to combine .extend with an object that already has __v (by the way, there are two underscores, not that document.__v++ affects the model anyway) in addition to using .increment .

When you use _.extend , it copies the __v property directly to the object, which seems to cause problems because Mongoose cannot find the old version inside. I did not dig deep enough to understand why this is specific, but you can get around it by adding delete req.body.__v .

Instead of searching and saving, you can also use .findByIdAndUpdate as two steps. Note that this does not use __v or increase it internally. As another answer and related error indicates, if you want to upgrade during the upgrade, you need to do it manually.

+3
source

Versions were implemented to reduce doc.save() by design (not Model.update, etc.). But if you want, you can try the following:

 {$set: {dummy: [2]}, $inc: { __v: 1 }} 

However, this was a confirmed error according to the link

Please confirm your version of the mongoose from the stage of the above problem.

Thanks:)

0
source

All Articles