2015-04-05 24 views
1

我在JavaScript中使用Parse查询时遇到问题。我想尝试使用多个doesNotMatchKeyInQuery函数,但它只允许使用最后一个函数。任何想法,我可以如何使代码这样的工作?忽略代码其他部分可能存在的错误。我以此为例解析多个doesNotMatchKeyInQuery

//Query 1 
var Class1 = Parse.Object.extend("Class1"); 
var class1Query = new Parse.Query(Class1); 
class1Query.equalTo("id", id1); 

//Query 2 
var Class2 = Parse.Object.extend("Class2"); 
var class2Query = new Parse.Query(Class2); 
class2Query.equalTo("id", id2); 

//Query 3 
var Class3 = Parse.Object.extend("Class3"); 
var class3Query = new Parse.Query(Class3); 
class3Query.equalTo("id", id3); 

//Bringing it all together 
var finalQuery = new Parse.Query("User"); 

//This is the part below I am talking about 
finalQuery.doesNotMatchKeyInQuery("objectId", "id1", class1Query); 
finalQuery.doesNotMatchKeyInQuery("objectId", "id2", class2Query); 
finalQuery.doesNotMatchKeyInQuery("objectId", "id3", class3Query); 

finalQuery.find({ 
    success: function (results) { 
     response.success(results); 
    }, 
    error: function (error) { 
     response.error(error); 
    } 
}); 

回答

-1

无法在单个请求中执行如此复杂的查询。但是,您可以提前获取不想匹配的密钥,然后从中构建辅助查询。 我已经写了基于上面的代码示例:

// Assuming we're actually dealing with 3 different classes, 
// and these can't be combined into a single query 
var class1Query = new Parse.Query('Class1'); 
class1Query.equalTo('id', id1); 
var class2Query = new Parse.Query('Class2'); 
class2Query.equalTo('id', id2); 
var class3Query = new Parse.Query('Class3'); 
class3Query.equalTo('id', id3); 
// Fetch the results from all three queries simultaneously 
Parse.Promise.when([ 
    class1Query.find(), 
    class2Query.find(), 
    class3Query.find() 
]).then(function(results) { 
    // results will contain three arrays of results 
    // We can now build a query where the objectId is not equal 
    // to any of the objectIds of the results 
    var ids = []; 
    results.forEach(function(set) { 
    set.forEach(function(obj) { 
     ids.push(obj.id); 
    }); 
    }); 
    return new Parse.Query('FinalClass').notContainedIn('objectId', ids).find(); 
}) 

我想提醒你,这个查询将不会高效地为大型数据集。 “不等于”查询永远不会很快,因为它们必须遍历表中的每个对象。如果有另一种方式来获取您的数据,我高度鼓励它。

+0

我其实认为这是行不通的。只有其中一个查询是填充结果而不是所有的查询。 – Midevilworm 2015-04-06 17:44:51

+0

其实,我明白了。对于任何想知道每个.find方法的人来说,都需要在函数返回中附带一个结果变量var。它看起来像这样 ])。然后(功能(results1,results2,results3){ – Midevilworm 2015-04-06 17:51:40

+0

@Midevilworm你能写出完整的答案吗? – Idan 2016-02-14 14:06:41