You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _includes/js/queries.md
+60-10
Original file line number
Diff line number
Diff line change
@@ -316,9 +316,17 @@ query.count({
316
316
});
317
317
</code></pre>
318
318
319
+
319
320
## Compound Queries
320
321
321
-
If you want to find objects that match one of several queries, you can use `Parse.Query.or` method to construct a query that is an OR of the queries passed in. For instance if you want to find players who either have a lot of wins or a few wins, you can do:
322
+
For more complex queries you might need compound queries. A compound query is a logical combination (e. g. "and" or "or") of sub queries.
323
+
324
+
Note that we do not support GeoPoint or non-filtering constraints (e.g. `near`, `withinGeoBox`, `limit`, `skip`, `ascending`/`descending`, `include`) in the subqueries of the compound query.
325
+
326
+
327
+
### OR-ed query constraints
328
+
329
+
If you want to find objects that match one of several queries, you can use `Parse.Query.or` method to construct a query that is an OR of the queries passed in. For instance if you want to find players who either have a lot of wins or a few wins, you can do:
322
330
323
331
<pre><codeclass="javascript">
324
332
var lotsOfWins = new Parse.Query("Player");
@@ -328,16 +336,58 @@ var fewWins = new Parse.Query("Player");
328
336
fewWins.lessThan("wins", 5);
329
337
330
338
var mainQuery = Parse.Query.or(lotsOfWins, fewWins);
331
-
mainQuery.find({
332
-
success: function(results) {
333
-
// results contains a list of players that either have won a lot of games or won only a few games.
334
-
},
335
-
error: function(error) {
339
+
mainQuery.find()
340
+
.then(function(results) {
341
+
// results contains a list of players that either have won a lot of games or won only a few games.
342
+
})
343
+
.catch(function(error) {
336
344
// There was an error.
337
-
}
338
-
});
345
+
});
339
346
</code></pre>
340
347
341
-
You can add additional constraints to the newly created `Parse.Query` that act as an 'and' operator.
342
348
343
-
Note that we do not, however, support GeoPoint or non-filtering constraints (e.g. `near`, `withinGeoBox`, `limit`, `skip`, `ascending`/`descending`, `include`) in the subqueries of the compound query.
349
+
### AND-ed query constraints
350
+
351
+
If you want to find objects that match all conditions, you normally would use just one query. You can add additional constraints to the newly created `Parse.Query` that act as an 'and' operator.
352
+
353
+
<pre><codeclass="javascript">
354
+
var query = new Parse.Query("User");
355
+
query.greaterThan("age", 18);
356
+
query.greaterThan("friends", 0);
357
+
query.find()
358
+
.then(function(results) {
359
+
// results contains a list of users both older than 18 and having friends.
360
+
})
361
+
.catch(function(error) {
362
+
// There was an error.
363
+
});
364
+
</code></pre>
365
+
366
+
Sometimes world is complexer than this simple example and you may need an compound query of sub queries. You can use `Parse.Query.and` method to construct a query that is an AND of the queries passed in. For instance if you want to find users in the age of 16 or 18 who have either no friends or at least 2 friends, you can do:
367
+
368
+
<pre><codeclass="javascript">
369
+
var age16Query = new Parse.Query("User");
370
+
age16Query.equalTo("age", 16);
371
+
372
+
var age18Query = new Parse.Query("User");
373
+
age18Query.equalTo("age", 18);
374
+
375
+
var friends0Query = new Parse.Query("User");
376
+
friends0Query.equalTo("friends", 0);
377
+
378
+
var friends2Query = new Parse.Query("User");
379
+
friends2Query.greaterThan("friends", 2);
380
+
381
+
var mainQuery = Parse.Query.and(
382
+
Parse.Query.or(age16Query, age18Query),
383
+
Parse.Query.or(friends0Query, friends2Query)
384
+
);
385
+
mainQuery.find()
386
+
.then(function(results) {
387
+
// results contains a list of users in the age of 16 or 18 who have either no friends or at least 2 friends
0 commit comments