Skip to content

Commit 37e9e97

Browse files
author
gofabian
committed
Add docs about Parse.Query.and()
1 parent 4838e92 commit 37e9e97

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

_includes/js/queries.md

+60-10
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,17 @@ query.count({
316316
});
317317
</code></pre>
318318

319+
319320
## Compound Queries
320321

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:
322330

323331
<pre><code class="javascript">
324332
var lotsOfWins = new Parse.Query("Player");
@@ -328,16 +336,58 @@ var fewWins = new Parse.Query("Player");
328336
fewWins.lessThan("wins", 5);
329337

330338
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) {
336344
// There was an error.
337-
}
338-
});
345+
});
339346
</code></pre>
340347

341-
You can add additional constraints to the newly created `Parse.Query` that act as an 'and' operator.
342348

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><code class="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><code class="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
388+
// results: (age 16 or 18) and (0 or >2 friends)
389+
})
390+
.catch(function(error) {
391+
// There was an error.
392+
});
393+
</code></pre>

0 commit comments

Comments
 (0)