parse-platformparse-serverparse-javascript-sdk

Multiple parallel Increments on Parse.Object


Is it acceptable to perform multiple increment operations on different fields of the same object on Parse Server ?

e.g., in Cloud Code :

node.increment('totalExpense', cost); node.increment('totalLabourCost', cost); node.increment('totalHours', hours); return node.save(null,{useMasterKey: true});

seems like mongodb supports it, based on this answer, but does Parse ?


Solution

  • Yes. One thing you can't do is both add and remove something from the same array within the same save. You can only do one of those operations. But, incrementing separate keys shouldn't be a problem. Incrementing a single key multiple times might do something weird but I haven't tried it.

    FYI you can also use the .increment method on a key for a shell object. I.e., this works:

    var node = new Parse.Object.("Node");
    node.id = request.params.nodeId;
    node.increment("myKey", value);
    return node.save(null, {useMasterKey:true});
    

    Even though we didn't fetch the data, we don't need to know the previous value in order to increment it on the database. Note that you don't have the data so can't access any other necessary data here.