1

I'm currently running the Parse cloud code background job, which involves querying all users and then running a number of functions for every user object that's returned. How do I set the query to only return the first ______ user objects, rather than all of them?

I know that if you wanted to only return the first result you'd do return usersQuery.first instead of return usersQuery.each. Is there an equivalent that only returns the first X number of results?

Parse.Cloud.job("mcBackground", function(request, status) {
    // ... other code to setup usersQuery ...
    Parse.Cloud.useMasterKey();

    var usersQuery = new Parse.Query(Parse.User);

    return usersQuery.each(function(user) {
            return processUser(user)
                .then(function(eBayResults) {
                    return mcComparison(user, eBayResults);
                });
        })
        .then(function() {
            // Set the job's success status
            status.success("MatchCenterBackground completed successfully.");
        }, function(error) {
            // Set the job's error status
            status.error("Got an error " + JSON.stringify(error));
        });
});
Andrew
  • 3,839
  • 10
  • 29
  • 42
  • `usersQuery.limit = // whatever you want for 'X'` – danh Nov 15 '15 at 03:50
  • @danh My objective is to break up the job into multiple iterations, each which run on a dif chunk of users, so the first version runs on user 1-1000, 2nd one runs on users 1001-2000, and so on. – Andrew Nov 16 '15 at 06:05
  • Okay. The normal pattern is to query for users that the job hasn't run on yet. Sometimes the evidence of that is clear, sometimes you create a column on user -- probably a date column, sometimes you can use the updatedAtCol. Then do a limit=1000 on that query. If there's a sequential col that's being queried, you can do an orderBy and a skip along with a limit to page through. – danh Nov 16 '15 at 06:13
  • Basing it on updatedAt sounds like a good idea. How would I test for this? I'm assuming it would be something like `usersQuery.notEqualTo(updatedAt, "whatever todays date is");`? How would I format the "todays date" part? – Andrew Nov 16 '15 at 06:32
  • Better to use greaterThan and see here for yesterday's date...http://stackoverflow.com/questions/5511323/javascript-yesterday – danh Nov 16 '15 at 06:34

2 Answers2

1

You can't combine .limit with .each unfortunately. I would suggest not using a background job for this at all, and instead running this logic on Heroku or another provider (even a local machine) using the parse npm module. This would allow you more flexibility and you won't need to break it in to 1,000 object chunks.

Fosco
  • 38,138
  • 7
  • 87
  • 101
0

Try using Parse's .limit() option:

Parse.Cloud.job("mcBackground", function(request, status) {
    // ... other code to setup usersQuery ...
    Parse.Cloud.useMasterKey();

    var usersQuery = new Parse.Query(Parse.User).limit(7);

    return usersQuery.each(function(user) {
            return processUser(user)
                .then(function(eBayResults) {
                    return mcComparison(user, eBayResults);
                });
        })
        .then(function() {
            // Set the job's success status
            status.success("MatchCenterBackground completed successfully.");
        }, function(error) {
            // Set the job's error status
            status.error("Got an error " + JSON.stringify(error));
        });
});
Stefan Dochow
  • 1,454
  • 1
  • 10
  • 11
  • This gives me this error: `TypeError: Object # has no method 'slice'`. – Andrew Nov 16 '15 at 06:10
  • Yeah, I see that now. I edited my answer. 'new Parse.Query(Parse.User).limit(x)' ight what you have been looking for. – Stefan Dochow Nov 16 '15 at 07:41
  • That the gives me this error: `Result: Got an error "Cannot iterate on a query with sort, skip, or limit."`. Seems parse doesnt give you many options for customizing queries. – Andrew Nov 19 '15 at 23:00