2

I'm trying to get the results of the following sql command in Parse.com using cloud code.

Select shop, count(1) from Membership group by shop;

Is there a way to do so? or I can only get the number of members from selecting each shop?

var query = new Parse.Query("Membership");
query.equalTo("shop",shop_id);
var promise = query.find().then(function(results){
        var number_of_membership_of_one_shop = results.leng
        return results;
    });
Ng Zhong Qin
  • 1,211
  • 2
  • 15
  • 28

1 Answers1

2

Parse unfortunately doesn't support group by.

You could first select all shops and then use a count query for each shop.

var query = new Parse.Query("Membership");
query.equalTo("shop",shop_id);

var promise = query.count().then(function(count){
    var number_of_membership_of_one_shop = count;
    return number_of_membership_of_one_shop;
});

If this is performing too many requests, you could select all the memberships and then count them on the client, but this will have a limit of 1000 so you may need to adopt some other techniques:

var query = new Parse.Query("Membership");
query.select("shop_id");
query.limit(1000);
var storeCounts = [];

queryObject.find({
    success: function (results) {
        for (var i = 0; i < results.length; i++) {
            var shopId = results[i].get('shop_id');

            if (!storeCounts[shopId]) {
                storeCounts[shopId] = 0;
            }

            storeCounts[shopId]++;
        }
    },
    error: function (error) {
        alert("Error: " + error.code + " " + error.message);
    }
});
Wayne Ellery
  • 7,888
  • 1
  • 28
  • 45