3

I have a cloud code which returns sometimes more than 1000 results , I am not getting how to chain query to get more than 1000 results in following code.

var User = Parse.Object.extend("Journal");
    var mainQuery = new Parse.Query(User);

    mainQuery.equalTo("user",username);     


    mainQuery.limit(1000);
    mainQuery.find({ 
        success: function(results) {
          count = 0;
          var avg = 0;

         console.log("self:"+results.length);
            for(j = 0; j < results.length ; j++)
            {
                var entry = results[j];

                if(strcmp1(day,"All") != 0)
                {
                    var dt = entry.get("created");

                    var weekDay = dt.getDay();
                    if(weekDay == day)
                    {
                       total += entry.get("Level");
                       count++;
                    }
                }
                else
                {
                    total += entry.get("Level");
                    count++;
                }
            }

            if(count > 1)
              avg = total / count;

            response.success(avg);  
        }
    });

limit function is used for raising the default limit of 100 which is max 1000 per query result return cycle .

rici
  • 234,347
  • 28
  • 237
  • 341
vishal dharankar
  • 7,536
  • 7
  • 57
  • 93
  • Of course, you're limiting it to 1000 results, `mainQuery.limit(1000);` – BernardoLima Sep 05 '14 at 14:21
  • BernardoLima there is a limit of 1000 limit function helps in raising the limit Which is default 100. – vishal dharankar Sep 05 '14 at 14:30
  • 1
    I think you want to call the query multiple times, but call `mainQuery.skip(1000 * )` before executing the query. Keep doing that until the query returns no items and you'll have retrieved everything. – Steve Ruble Sep 05 '14 at 14:37

1 Answers1

5

I am using this code presently. This can fetch more than 1000 results. The values are sorted according to "objectId". You can sort according to "createdAt" by changing accordingly. Final results are stored in result array

      var result = [];
      var processCallback = function(res) {
                result = result.concat(res);
                if (res.length == 1000) {
                  process(res[res.length-1].id);
                  return;
                }   
                    //To print all the ids
                    for(var i=0;i<result.length;i++){
                        console.log(result[i].id);
                    }
                }

        var process = function(skip) {
            var query = new Parse.Query("ObjectName");

            if (skip) {
              query.greaterThan("objectId", skip);
            }
            query.limit(1000);
            query.ascending("objectId");
            query.find().then(function querySuccess(res) {
              processCallback(res);
            }, function queryFailed(error) {
                });
          }
      process(false);
Technoid
  • 435
  • 3
  • 11