Skip to content

Add docs about Parse.Query.and() #445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 60 additions & 10 deletions _includes/js/queries.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,17 @@ query.count({
});
</code></pre>


## Compound Queries

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:
For more complex queries you might need compound queries. A compound query is a logical combination (e. g. "and" or "or") of sub queries.

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.


### OR-ed query constraints

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:

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

var mainQuery = Parse.Query.or(lotsOfWins, fewWins);
mainQuery.find({
success: function(results) {
// results contains a list of players that either have won a lot of games or won only a few games.
},
error: function(error) {
mainQuery.find()
.then(function(results) {
// results contains a list of players that either have won a lot of games or won only a few games.
})
.catch(function(error) {
// There was an error.
}
});
});
</code></pre>

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

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.
### AND-ed query constraints

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.

<pre><code class="javascript">
var query = new Parse.Query("User");
query.greaterThan("age", 18);
query.greaterThan("friends", 0);
query.find()
.then(function(results) {
// results contains a list of users both older than 18 and having friends.
})
.catch(function(error) {
// There was an error.
});
</code></pre>

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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would switch complexer to more complex. I'm not entirely sure complexer is a word, but I could be wrong.

Additionally, I think the first and second sentence can be morphed together to explain that the simple query above may not be sufficient in all cases and Parse.Query.and solves the problem.

Here is an example of what this could look like:

For more complex queries, you can use the Parse.Query.and method to return a query that is an AND of the input queries. For instance if you want to find users at the age of 16 or 18 who have either no friends or at least 2 friends, you can do:


<pre><code class="javascript">
var age16Query = new Parse.Query("User");
age16Query.equalTo("age", 16);

var age18Query = new Parse.Query("User");
age18Query.equalTo("age", 18);

var friends0Query = new Parse.Query("User");
friends0Query.equalTo("friends", 0);

var friends2Query = new Parse.Query("User");
friends2Query.greaterThan("friends", 2);

var mainQuery = Parse.Query.and(
Parse.Query.or(age16Query, age18Query),
Parse.Query.or(friends0Query, friends2Query)
);
mainQuery.find()
.then(function(results) {
// results contains a list of users in the age of 16 or 18 who have either no friends or at least 2 friends
// results: (age 16 or 18) and (0 or >2 friends)
})
.catch(function(error) {
// There was an error.
});
</code></pre>