mongodb递增数字虽然我递减了吗?

时间:2017-10-12 09:19:39

标签: mongodb

我在第一个查询中将sp_amount增加了1.6。然后我把它减少到-1.6它仍然增加了价值!如何设置正确?

用于INCREMENT的查询:

  

db.userwalletcreditlogs.findOneAndUpdate({transaction_id:   " 8zioepbvwx7ua7vxse2u01wcdi"},{$设置:{stripe_transfer_id   :" tr_1BBkxaCOEurZs8WVkXBqJwUT"},$ inc:{sp_amount:1.6}})

输出:

  

" sp_amount" :181.6,
    " stripe_transfer_id" :" tr_1BBkxaCOEurZs8WVkXBqJwUT",

用于DECREMENT的查询:

  

db.userwalletcreditlogs.findOneAndUpdate({transaction_id:" 8zioepbvwx7ua7vxse2u01wcdi"},{$ set:{stripe_transfer_id:" tr_1BBkxaCOEurZs8WVkXBqJwUT"},$ inc:{sp_amount:-1.6}})

输出:

  

" sp_amount" :183.2,
    " stripe_transfer_id" :" tr_1BBkxaCOEurZs8WVkXBqJwUT",

HERE is the screenshot of my mongoshell

1 个答案:

答案 0 :(得分:2)

PROPERTYMETER将在应用操作之前返回对象。请参阅下面的结果:

PROPERTYMETER

如果您想在更新应用后获取对象,则需要传递PROPERTYMETER选项,请参阅下面的相同示例:

private void startClock(){
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                while (!isInterrupted()) {
                    Thread.sleep(1000);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Calendar c = Calendar.getInstance();

                            int hours = c.get(Calendar.HOUR_OF_DAY);
                            int minutes = c.get(Calendar.MINUTE);
                            int seconds = c.get(Calendar.SECOND);

                            String curTime = String.format("%02d  %02d  %02d", hours, minutes, seconds);
                            clock.setText(curTime); //change clock to your textview
                        }
                    });
                }
            } catch (InterruptedException e) {
            }
        }
    };

    t.start();
}

有关详情,请查看文档 - https://docs.mongodb.com/v3.2/reference/method/db.collection.findOneAndUpdate/#definition