本文介绍了如何增加猫鼬中的数字值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
StackOverflow的人们,我在想这个问题,我该如何增加
猫鼬的数值?我尝试了下面的代码,尽管无法正常工作.我正在尝试在提交表单时将值增加1.下面是我的代码:
People of StackOverflow, I am burning with the question, how do I increment a
Number value in Mongoose? I have tried the code below, though it is not working. I am trying to increment a value by one on the submission of a form. Below is my code:
app.post('/like', function (req, res) {
var id = req.body.id;
var query = {_id: id};
var post = Meme.findOne(query);
Meme.findOneAndUpdate(post, post.likes: post.likes+1)
});
非常感谢您的宝贵帮助!
Thanks so much for your valuable help!
推荐答案
您可以将$inc
用于此目的.
You can use $inc
for this purpose.
尝试一下:
var id = req.body.id;
Meme.findOneAndUpdate({_id :id}, {$inc : {'post.likes' : 1}}).exec(...);
有关$ inc的更多信息,请阅读 MongoDB $ inc文档
For more info on $inc, Please read MongoDB $inc documentation
这篇关于如何增加猫鼬中的数字值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!