Reputation: 1454
I need to do the following query in Parse.com (pseudo language):
select all posts where
(expired < today OR expired not exists)
AND
(owner is {user} OR commenter contains {user})
I know in Javascript I can create the two OR queries using Parse.Query.or
, but since there doesn't seems to be a Parse.Query.and
variant, I don't know how I can combine these two OR queries into a single query.
Other people have suggested to simply use equalTo
multiple times to create AND statements, but this doesn't work for a composed OR query.
Upvotes: 1
Views: 6901
Reputation: 155
You can use both AND with OR like this. Attributes in the AND being the equalTo valuables.
const mainQuery = Parse.Query.or(
Parse.Query.and(lotsOfWins, fewWins2),
Parse.Query.and(lotsOfWins2, fewWins)
);
You can add as many as you like..
const lotsOfWins = new Parse.Query("CLASS");
lotsOfWins.equalTo('user', CurrentUser);
const fewWins = new Parse.Query("CLASS");
fewWins.equalTo('recipient', CurrentUser);
Upvotes: 1
Reputation: 42109
I'm not familiar with Parse and this is too long for a comment, but see below from the documentation; the last sentence in particular seems to be what you're after.
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:
var lotsOfWins = new Parse.Query("Player"); lotsOfWins.greaterThan("wins", 150); 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) { // There was an error. } });
You can add additional constraints to the newly created Parse.Query that act as an 'and' operator.
Upvotes: 5