Search code examples
parse-platformparse-cloud-code

Passing a big amount of data to a Parse Query


Im working with an application that uses a table with (name, gender) as a schema. This is used to retrieve the gender of a given name. So what im struggling to achieve here is to give parse an Array with thousands of names and return as result the gender for each name. Can I make it with one Parse call only?

I tried using Parse.Query.or with a query for each name but sadly this only supports 10 different queries.

Any ideas?


Solution

  • Parse can return only 1000 records per query at most. So if you have N number of names. It will require minimum N/1000 queries.

    Reference for parse limitations http://profi.co/all-the-limits-of-parse/

    To optimally make queries you can make a batch of 1000 names and make query to Parse and set limit to 1000. Then iterate all the records and generate a dictionary. But to make it even more faster you can make those calls asynchronously.

    Example

    // function to fetch gender of list of names
    function fetchGenderOfNames(arrayOfNames, successCallback, errorCallback){
        var numberOfBatches = (arrayOfNames.length/1000 + (arrayOfNames.length % 1000)? 1 : 0),
            callCount = 0,
            genderOfNames = {},
            errorFlag = false;
        function querySuccessCallback(records){
            if(!errorFlag){
                records.forEach(function(record){
                    genderOfNames[record.get('name')] = record.get('gender');
                });
                callCount++;
                if(callCount == numberOfBatches){
                    successCallback(genderOfNames);
                }
            }
        }
        function queryErrorCallback(error){
            if(!errorFlag){
                errorFlag = true;
                errorCallback(error);
            }
        }
        for(var arrayIndex = 0; arrayIndex < arrayOfNames.length; arrayIndex += 1000){
            if(!errorFlag){
                var genderQuery = new Parse.Query('Table_name');
                // following match will be case sensitive
                genderQuery.containedIn('name', arrayOfNames.slice(arrayIndex, arrayIndex + 1000);
                genderQuery.limit(1000);
                genderQuery.find({success: querySuccessCallback, error: queryErrorCallback});
            }
        }
    }