Monday, 13 March 2017

Increment and decrement Rethinkdb

RethinkDB Increment and Decrement Single Value

Need some help, I'm learning Nodejs and Rethinkdb.

Currently i'm using this Rethinkdb code to perform the Update operation.

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});
    }
});

Now the question is: How can i update a value in a row by Incrementing its value? For example lets say its about the stock of a car. so in a row i'll have:

    {
    "code":  "001" ,
    "brand":  "Ferrari" ,
    "stock":  "2"
    }

Now lets say i make a form to control how many cars will be added or removed from the stock, So for example lets say i want to add 3 new ferrari's. how can i update the value stock, incrementing the stock by 3 for a total of 5 and not replacing 2 for 3. also lets say i sold 2 ferraris, how can i decrease the value so the stock value will be 0.

and Please excuse my poor english. not my natal language.



via RocketFuel

No comments:

Post a Comment