0
我想不出一个简单的用例findAndModify的.....MongoDB的findAndModify更新所需的错误
db.collection('products').findAndModify(
{
query: { _id: new o_id(cart[i]._id) },
update: { $inc: { sold: qty, onstock: (-1*qty) } },
new: true
},function(e,d) {
if (e) return console.log('findAndModify error',e);
});
但上面的一贯返回以下错误..
{
[ MongoError: need remove or update ]
name: 'MongoError',
message: 'need remove or update',
ok: 0,
errmsg: 'need remove or update'
}
上午我错过了什么?
也试过在回答表明从zangw
db.collection('products').findAndModify(
{ _id: new o_id(cart[i]._id) },
{ $inc: { sold: qty, onstock: (-1*qty) } },
{ new: true },function(e,d) {
if (e) { return console.log('updateStock findAndModify error',e); }
console.log('updated',d);
});
现在得到另一个错误
findAndModify error
{ [MongoError: exception: nextSafe():
{ $err: "Can't canonicalize query: BadValue bad sort specification",
code: 17287 }]
name: 'MongoError', message: 'exception: nextSafe():
{ $err: "Can\'t canonicalize query: BadValue bad sort specification",
code: 17287
}',
errmsg: 'exception: nextSafe():
{ $err: "Can\'t canonicalize query: BadValue bad sort specification",
code: 17287 }',
code: 13106,
ok: 0 }
正在运行的MongoDB 2.6.11
解决方案,通过@Blakes七(的建议在findAndModify上弃用)
db.collection('products').findOneAndUpdate(
{ _id: new o_id(cart[i]._id) },
{ $inc: { sold: qty, onstock: (-1*qty) } },
function(e,d) {
if (e) { return console.log('updateStock findAndModify error',e); }
findAndModify将更新的文档作为d.value返回。
谢谢回复。 – user3094755
您建议失败的格式,将用新tx更新问题。 – user3094755
@ user3094755,添加'{_id:'降序'},因为我的回答表明 – zangw