我有一个问题 - 不确定我做错了什么或者是一个错误.我有一些产品 - 每个产品都有一系列的变化.我想查看一些数据并在这些变体中加载它,但我遇到了一些"VersionError:找不到匹配的文档"错误.
以为我有一个竞争条件(我按顺序为我修改的每个变体保存相同的文档)我使用了asyc.eachSeries()但这没有帮助.一次加载导致文档错误的错误不会产生错误,所以它似乎与某些竞争条件有关但我无法追踪它.
架构:
var Product = new Schema({
title: {
type: String,
},
variations: {
type: Array
}
});
Run Code Online (Sandbox Code Playgroud)
示例代码:
// 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); …Run Code Online (Sandbox Code Playgroud) 当我尝试保存我的文档时,我收到一个VersionError: No matching document found错误,类似于这个问题.
阅读此博客文章后,似乎问题在于我的文档版本控制.我搞乱了一个阵列,所以我需要更新版本.
但是,打电话document.save()对我不起作用.当我在调用之前和之后注销文档时save(),document._v是同样的事情.
我也尝试过document._v = document._v++这也行不通.
码
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) …Run Code Online (Sandbox Code Playgroud) 我正在使用Node.js和MongoDB/Mongoose开发Web应用程序.我们最常用的Model,Record,有许多子文档数组.例如,其中一些包括"评论","预订"和"订阅者".
在客户端应用程序中,每当用户点击"删除"按钮时,它就会触发针对该特定注释的删除路由的AJAX请求.我遇到的问题是,当许多AJAX调用同时进入时,Mongoose在某些(但不是全部)调用中失败并显示"Document not found"错误.
这种情况只发生在一次快速拨号和多次拨号时.我认为这是由于Mongoose中的版本导致文档冲突.我们目前的删除流程是:
Record.findById()comment.remove())record.save()我找到了一个解决方案,我可以手动更新集合Record.findByIdAndUpdate,然后使用$pull运算符.但是,这意味着我们不能使用任何mongoose的中间件并完全松开版本控制.我越是想到它,我越发明这种情况会发生,我将不得不使用Mongoose的包装函数,如findByIdAndUpdate或findAndRemove.我能想到的唯一其他解决方案是将删除尝试放入while循环并希望它能够正常工作,这似乎是一个非常糟糕的修复.
使用Mongoose包装器并没有真正解决我的问题,因为它根本不允许我使用任何类型的中间件或钩子,这基本上是使用Mongoose的巨大好处之一.
这是否意味着Mongoose对于快速编辑的任何东西都是无用的,我可能只使用本机MongoDB驱动程序?我误解了猫鼬的局限吗?我怎么能解决这个问题?