Version Error: no matching doc found
See original GitHub issueI have an issue - not sure if I am doing something wrong or it’s a bug. I have some products - each of these has an array of variations. I want to go through some data and load it in these variations but I experience a number of ‘VersionError: No matching document found’ errors.
Thinking I was having a race condition (I am sequentially saving the same document for each of its variations that I modify) I used asyc.eachSeries() but that did not help. Loading the error causing documents one at the time does not yield the error so it seems related to some race condition but I cannot track it down.
Schema:
var Product = new Schema({
title: {
type: String,
},
variations: {
type: Array
}
});
Sample code:
// Some data to load - the 'variant' is the index of the variations array above
var records = [{
code: 'foo',
id: '50ba9c647abe1789f7000073',
variant: 0
}, {
code: 'bar',
id: '50ba9c647abe1789f7000073',
variant: 1
}, {
code: 'foobar',
id: '50ba9c647abe1789f7000073',
variant: 2
}];
var iterator = function(item, cb) {
Product.findById(item.id).exec(function(err, product) {
if(err) {
return cb(err);
}
if (product) {
product.variations[item.variant].code = item.code.trim();
product.markModified('variations');
product.save(function(err, p) {
return cb(err);
});
} else {
return cb('Missing product');
}
});
};
async.eachSeries(records, iterator, function(err) {
process.exit(1);
});
Issue Analytics
- State:
- Created 10 years ago
- Comments:21 (2 by maintainers)
Top Results From Across the Web
Mongoose - Version Error: No matching document found for id
This change is rejected because the version fields no longer match. How this works is that, behind the scenes, mongoose's save() method adds...
Read more >Mongoose - Version Error: No matching document found for id
I make a fetch API request from a different web app to POST the csv strings for a particular Post . The code...
Read more >Version Error: No matching document found for id-mongodb
This is the error you're seeing. If you want to force the object to update regardless of version control in this particular instance...
Read more >Mongoose, how should I handle the error "version ... - Reddit
Mongoose, how should I handle the error "version error no matching document found"? ... This usually occurs during concurrent edits to the same...
Read more >MongoDB and Mongoose - Create and Save a Record (SAVE ...
hello, follow this link https://stackoverflow.com/questions/45223025/mongoose-version-error-no-matching-document-found-for- ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi! I have some problem too! I noticed that usage of logging
modifiedPaths
leads to different output data.First saving: [‘Groups’]
More saving: [‘ModifiedAt’, ‘__v’, ‘groups’]
Perhaps the problem is that the new requires do not use current version
__v
field (they use remaining in “client” memory).SOLVED I deleted version property from requests for update.
delete req.body.__v;
@dciccale after taking a closer look at your code, yep, that’s expected behavior and a classic example of how versioning is expected to work - overwriting an array when the array changed underneath you is a version error. You can disable versioning or use the skipVersioning to selectively skip a field for versioning if you’re sure you’re not worried about conflicting changes.