当类具有>时,从parse.com检索密钥的唯一值。 1000个物体

时间:2014-10-29 01:12:14

标签: javascript parse-platform

在解析中我有一个名为" TestScore"的类。每个对象都有一个名为" quizName"。

的键

我需要获得一系列独特的" quizName"值。我写了下面的查询" TestClass"并循环搜索结果,寻找唯一的" quizName"值。

起初似乎做了这个工作。但后来我意识到返回的对象的最大数量是1000.很快就会存储超过1000个对象,这意味着这种方法不能保证我最终会得到所有值。

function loadTests(){
    //create an array to hold each unique test name as we find them
    var uniqueEntries = [];

    //query parse to return TestScore objects
    var TestScore = Parse.Object.extend("TestScore");
    var query = new Parse.Query(TestScore);
    query.limit(1000)  //added this after realizing that the default query limit is only 100 
    query.find({
      success: function(testScore) {
          $(testScore).each(function(index, score) {

             //here I loop though all of the returned objects looking at the "quizName" for each
             if($.inArray(score.get("quizName"), uniqueEntries) === -1) {

               //if the quiz name is not already in the "uniqueEntries" array, I add it to the array
                uniqueEntries.push(score.get("quizName"));
             }
          });
          //do stuff with quiznames here...., add them as options in select boxes mostly
      }
    });
}

我看了{Parse.Query} notContainedIn(key, values)看起来很有希望但是无法弄清楚我是否可以在找到数据时为数组添加值。看起来我必须有一个数组开始(击败整点。)

这部分文档" {Parse.Query}返回查询,因此您可以链接此调用。"让我觉得我可以将查询链接在一起以获得我需要的东西,但这看起来并不高效。

如何检索键" quizName"的唯一值?当我的班级有> 1000件物品?

2 个答案:

答案 0 :(得分:1)

我确信你现在已经很久没过了,但我知道的唯一方法就是通过对每个查询使用.skip(#)值来使用一个接一个的查询。所以得到1000,然后用.skip(1000)再次查询,连接第一个列表和第二个列表中的项目,然后再次使用.skip(2000)等查询...

请注意,我认为跳过值为10,000的限制。不要相信我的意思,只是指出一些我认为正确的事情,你应该确认它是否适用于你的情况。

答案 1 :(得分:0)

我最终找到了tutorial online,我能够修改并提出以下内容。这有效地将返回限制设置为10,000而不是1,000,并允许为查询设置多个不同的参数。

我的更改肯定可以更好地编写,可能作为options对象或类似对象,但它可以满足我的需求。

您可以看到working demo here

function getStuff(){
    // here we will setup and call our helper functions with callbacks to handle the results
    var scheme =['SOTest',true]; // return all objects with value `true` in the `SOTest` column
    // var scheme =['descending','createdAt']; // return all objects with sort order applied
    // var scheme ='';  // or just return all objects
    // see `findChunk()` below for more info
    var Remark = Parse.Object.extend("Remark");
    schemePromise(Remark, scheme).done(function (all) {

        console.log('Found ' + all.length+' Remarks');

        $.each( all, function(i, obj){
            $('#test').append(obj.get('Remark') +'<br>');

        });
    })
        .fail(function (error) {
            console.log("error:  " + JSON.stringify(error));
    });

}


getStuff(); // call our function

// helper functions used to get around parse's 1000 query limit
// raises the limit to 10,000 by using promises 
function findChunk(model, scheme, allData) {
    // if `scheme` was an empty string, convert to an array
    // this is the default and returns all objects in the called class
    if(scheme==''){ ['scheme',''] };
     // will return a promise
    var limit = 1000;
    var skip = allData.length;
    var findPromise = $.Deferred();
    var query = new Parse.Query(model);
    // to get all objects from the queried Class then sort them by some column
    // pass `scheme` as an array like [ sort method, column to sort ] 
    if (scheme[0]=='descending') query.descending(scheme[1]);
    else if (scheme[0]=='ascending') query.ascending(scheme[1]);
    // to limt results to objects that have a certain value in a specific column
    // pass `scheme` as an array like [ column name, value ] 
    else query.equalTo(scheme[0], scheme[1]);
    // more options can easily be built in here using `scheme`
    query
        .limit(limit)
        .skip(skip)
        .find()
        .then(function (results) {
        findPromise.resolve(allData.concat(results), !results.length);
    }, function (results) {
        findPromise.reject(error);
    });
    return findPromise.promise();
}
function schemePromise(model, scheme, allResults, allPromise) {
    // find a scheme at a time
    var promise = allPromise || $.Deferred();
    findChunk(model, scheme, allResults || [])
        .done(function (results, allOver) {
        if (allOver) {
            // we are done
            promise.resolve(results);
        } else {
            // may be more
            schemePromise(model, scheme, results, promise);
        }
    })
        .fail(function (error) {
        promise.reject(error);
    });
    return promise.promise();
}