Mysql(或者sqlite), Mongo中update Column + 1

Mysql(或者sqlite), Mongo中update Column + 1

有类似以下需求,在数据库表里有一个字段,记录了一个count,然后又时候需要在count的基础上加上某个数字,比如1。看到这个需求后,由于是要操作mongo数据库,发现mongo update的时候$inc可以实现这个结果,但是没想起Mysql或者sqlite要怎么操作,总不能query拿到count后再update吧。在stackoverflow上找到了以下的解决方案,果然是简单方便。

update table set column = column + n

这是里相关网址
http://stackoverflow.com/questions/2762851/increment-a-database-field-by-1

http://stackoverflow.com/questions/3866524/mysql-update-column-1

sqlite> create table test2 (count integer, id text);
sqlite> insert into test2 values(10, 'aa');
sqlite> insert into test2 values(0, 'bb');
sqlite> select * from test2;
10|aa
0|bb
sqlite> update test2 set count = count + 1 where id = 'aa'
   ...> ;
sqlite> select * from test2;
11|aa
0|bb
sqlite> update test2 set count = count + 2 where id = 'aa';
sqlite> select * from test2;
13|aa
0|bb
sqlite>

对于mongo里的解决方案

{ $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } }

官方文档如下
https://docs.mongodb.org/manual/reference/operator/update/inc/

在mongoose里可以使用findOneAndUpdate.

var update = {$inc:{rate:1}}; //重点
UserSing.model.findOneAndUpdate({'user': userId}, update, {new: true, upsert: true}, function(err, sing){
    if(err) {
        return next(new CommonError(ErrCode.invalid_parameter, '修改状态失败'));
        console.error(condition + err);
    }
    console.error(sing);
    res.send(sing);
});

http://stackoverflow.com/questions/8621948/doing-inc-with-mongoose

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值