Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 单个值的增量和减量_Node.js_Rethinkdb - Fatal编程技术网

Node.js 单个值的增量和减量

Node.js 单个值的增量和减量,node.js,rethinkdb,Node.js,Rethinkdb,db递增和递减单个值 需要一些帮助,我正在学习Nodejs和referencedb 目前我正在使用这段代码来执行更新操作 r.table('items').get(id).update(item).run(self.connection, function(err, result) { if(result && result.replaced === 1) { res.send(item); } else if(err) {

db递增和递减单个值

需要一些帮助,我正在学习Nodejs和referencedb

目前我正在使用这段代码来执行更新操作

r.table('items').get(id).update(item).run(self.connection, function(err, result) {
    if(result && result.replaced === 1) {
        res.send(item);
    }
    else if(err) {
        debug("[ERROR] updateItem %s:%s\n%s", err.name, err.msg, err.message);
        res.send({error: 'An error occurred when updating the item with id: ' + id});
    }
    else {
        debug("[ERROR] updateItem (%s): %j", id, result);
        res.send({error: 'An error occurred when updating the item with id: ' + id});
    }
});
现在的问题是:如何通过增加行中的值来更新它?例如,让我们说它是关于一辆汽车的库存。因此,接下来我将有:

    {
    "code":  "001" ,
    "brand":  "Ferrari" ,
    "stock":  "2"
    }
现在让我们假设我制作一个表格来控制库存中添加或删除的汽车数量。比如说,我想增加3辆新的法拉利。如何更新值stock,将stock增加3,总共增加5,而不是将2替换为3。也让我们假设我卖了2辆法拉利,我如何才能降低价值,使股票价值将为0


请原谅我的英语不好。不是我的母语。

如果我理解正确,下面是你能做的。要添加,请使用以下内容:

r.table("items").get(id).update({
    stock: r.row("stock").add(2)
}).run(self.connection, callback);
减去:

r.table("items").get(id).update({
    stock: r.row("stock").sub(3)
}).run(self.connection, callback);
更新

因此,数据库中的值实际上是字符串。你需要先强迫他们。这应该起作用:

r.table("items").get(id).update({
    stock: r.row("stock").coerceTo("number").add(2).coerceTo("string")
}).run(self.connection, callback);

如果我理解正确,下面是你能做的。要添加,请使用以下内容:

r.table("items").get(id).update({
    stock: r.row("stock").add(2)
}).run(self.connection, callback);
减去:

r.table("items").get(id).update({
    stock: r.row("stock").sub(3)
}).run(self.connection, callback);
更新

因此,数据库中的值实际上是字符串。你需要先强迫他们。这应该起作用:

r.table("items").get(id).update({
    stock: r.row("stock").coerceTo("number").add(2).coerceTo("string")
}).run(self.connection, callback);

首先谢谢。但是当我尝试代码时,它只是在原始值旁边加2,所以我得到的不是2+2=4,而是:22So,数据库中的值是一个字符串,而不是int。对不起,我没有注意到这一点。你想这样吗?也许只是用
parseInt
转换它实际上不起作用,但我更新了我的答案。谢谢!!很好,首先谢谢。但是当我尝试代码时,它只是在原始值旁边加2,所以我得到的不是2+2=4,而是:22So,数据库中的值是一个字符串,而不是int。对不起,我没有注意到这一点。你想这样吗?也许只是用
parseInt
转换它实际上不起作用,但我更新了我的答案。谢谢!!效果很好。