如何让Parse查询只返回x个结果

How to have a Parse query only return x number of results?

本文关键字:返回 结果 查询 Parse      更新时间:2023-09-26

我目前正在运行解析云代码后台作业,该作业涉及查询所有用户,然后为返回的每个用户对象运行许多函数。如何将查询设置为只返回前______个用户对象,而不是全部返回?

我知道,如果你只想返回第一个结果,你会做return usersQuery.first而不是return usersQuery.each。是否存在只返回前X个结果的等价项?

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));
        });
});

不幸的是,您不能将.limit.each组合。我建议根本不使用后台作业,而是使用解析npm模块在Heroku或其他提供程序(甚至是本地机器)上运行此逻辑。这将为您提供更大的灵活性,并且您不需要将其分解为1000个对象块。

尝试使用Parse的.limit()选项:

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));
        });
});