我有一个包含多个对象的数组,我想用qty
减少订单中_id
的值。这是我的努力:
funcs.updateOrder=function(request, reply){
Order.update({
"order._id" : request.payload.orderid
}, {
"$set" : {
"$inc":{"order.$.qty" : {qty:request.payload.qty}},
"order.$.reason" : request.payload.reason,
}
},
function(err) {
if(err){
reply(err).code(500);
}
else{
reply({"message":"success"});
}
});
};
这是我的mongodb架构: -
{
"_id": "57206feb05a533ac0f84447e",
"createdAt": "2016-04-27T07:53:15.108Z",
"updatedAt": "2016-04-27T07:53:15.108Z",
"orderno": "ORDER-NO-04-2016-2",
"order": [
{
"qty": -1,
"model": "56dd2a2adcb3dde8067906dc",
"vendorname": "57079f50182dd95c16c2d790",
"partname": "57206ddd566d86580ec2d480",
"make": "56dd2a29dcb3dde80679062e",
"_id": "57206feb05a533ac0f844482",
"reason": "unable to delivered"
},
{
"qty": 12,
"model": "56dd2a2adcb3dde8067906dc",
"vendorname": "57079f50182dd95c16c2d790",
"partname": "57206ddd566d86580ec2d480",
"make": "56dd2a29dcb3dde80679062e",
"_id": "57206feb05a533ac0f844481",
"reason": "unable to delivered"
},
{
"qty": 66,
"model": "56dd2a2adcb3dde8067906f1",
"vendorname": "5710be56dc475f640e83a0ba",
"partname": "57206ddd566d86580ec2d480",
"make": "56dd2a29dcb3dde80679062e",
"_id": "57206feb05a533ac0f844480",
"reason": null
},
{
"qty": 55,
"model": "56dd2a2adcb3dde806790704",
"vendorname": "57079f50182dd95c16c2d790",
"partname": "57206ddd566d86580ec2d480",
"make": "56dd2a29dcb3dde80679062e",
"_id": "57206feb05a533ac0f84447f",
"reason": null
}
],
"__v": 0
}
答案 0 :(得分:1)
$inc
运算符是一个独立的field update operator,并不像 $set
那样用作嵌入式运算符完成。你需要将两者分开:
funcs.updateOrder = function(request, reply){
Order.update(
{ "order._id" : request.payload.orderid },
{
"$set": { "order.$.reason": request.payload.reason },
"$inc": { "order.$.qty": request.payload.qty }
}, function(err) {
if (err){
reply(err).code(500);
}else{
reply({"message": "success"});
}
});
});