0

I have Inventory table in my Parse database with two relevant fields: productId and quantity. When a shipment is received, a record is created containing the productId and quantity. Similarly, when a sale occurs, an inventory record is made with the productId and quantity (which will be negative, since the inventory will decrease after the sale).

I would like to run a group by/ sum aggregate query on the Inventory table with Parse Cloud Code that outputs a dictionary containing unique productIds as the keys and the sum of the quantity column for those Ids as the values.

I have seen a number of old posts saying that Parse does not do this, but then more recent posts refer to Cloud Code such as averageStart in the Cloud Code Guide: https://parse.com/docs/cloud_code_guide

However, it seems that Parse.Query used in averageStars has a maximum limit of 1000 records. Thus, when I sum the quantity column, I am only doing so on 1000 records rather than the whole table. Is there a way that I can compute the group by/ sum across all the records in the inventory table?


For example:

Inventory Table

productId quantity

Record 1: AAAAA 50

Record 2: BBBBB 40

Record 3: AAAAA -5

Record 4: BBBBB -2

Record 5: AAAAA 10

Record 6: AAAAA -7

Output dictionary: {AAAAA: 48, BBBBB: 38}

1

2 Answers 2

4

You can use Parse.Query.each(). It has no limit. If your class has too many entries it will timeout though.

See docs

e.g.:

var totalQuantity = 0;
var inventoryQuery = new Parse.Query("Inventory");
inventoryQuery.each(
    function(result){
        totalQuantity += result.get("quantity");
    }, {
        success: function() {
            // looped through everything
        },
        error: function(error) {
            // error is an instance of Parse.Error.
        }
    });
});

If it times out, you have to build something like this.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!!! It works perfectly. I modified the code slightly to output the dictionary.
3

In case you want to see the code with the dictionary:

Parse.Cloud.define("retrieveInventory", function(request, response) {
  var productDictionary ={};
  var query = new Parse.Query("Inventory");
  query.equalTo("personId", request.params.personId);
  query.each(
     function(result){
        var num = result.get("quantity");
        if(result.get("productId") in productDictionary){
             productDictionary[result.get("productId")] += num;
        }
        else{
             productDictionary[result.get("productId")] = num;
        }
    }, {
        success: function() {
            response.success(productDictionary);
        },
        error: function(error) {
            response.error("Query failed. Error = " + error.message);
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.