我正在尝试使用NodeJs / Mongoose代码在mongo db中增加一个整数值。但是,它什么也没做。
这是Mongo文档,我试图将值ratings.likes增加1
{
"_id" : "some-recipe",
"name" : "Some Recipe",
"ratings" : {
"votes" : 234,
"stars" : 4.5,
"likes" : 3
}
}
以下是更新类似计数的代码
router.put('/:id/like', function(req, res){
var id = req.params.id;
console.log('Register like with id:' + id);
models.Recipe.update(id, {$inc: {"ratings.likes":1}}, function (err, responsePayload) {
if(err){
console.log(err)
res.json({error:err});
}
if (responsePayload == null){
console.log("Encountered error : " + err);
res.status(404).json({error: 'Resource (' + id + ') Not found'})
} else {
console.log("Got this, after like :" + JSON.stringify(responsePayload));
res.json(responsePayload);
}
});
});
这是来自更新声明的共鸣
{
"ok": 0,
"n": 0,
"nModified": 0
}