Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do an efficient boolean "false or undefined" query in Parse?

I have a Boolean column on one of my Parse entities, and I want to query for all rows that have this column either explicitly set to false, or left to its default undefined value.

I can't do equalTo('myBoolColumn', false), because it doesn't return rows that have an undefined value for this column.

I'd rather not do a notEqualTo('myBoolColumn', true), because the Parse documentation states that notEqualTo queries are not efficient because they can't take advantage of indexes.

The documentation suggests using containedIn instead, but it feels wrong to write a containedIn('myBoolColumn', [false, undefined]) query to achieve the expected result.

It seems like notEqualTo boolean queries could still be indexed, but I did not find an authoritative source that confirms this, and I don't know how to test if this query uses an index or not.

So which one should I use: notEqualTo('myBoolColumn', true) or containedIn('myBoolColumn', [false, undefined])?

like image 550
Pascal Bourque Avatar asked Aug 24 '15 16:08

Pascal Bourque


2 Answers

You want to combine two queries like so:

var falseQuery = new Parse.Query('YourClass');
falseQuery.equalTo('myBoolColumn', false);

var undefinedQuery = new Parse.Query('YourClass');
undefinedQuery.doesNotExist('myBoolColumn');

//this is the query you should run
var query = Parse.Query.or(falseQuery, undefinedQuery);
query.find({....});
like image 166
AlexKoren Avatar answered Sep 24 '22 04:09

AlexKoren


Since you're checking for whether it's false or undefined, you could also more simply do:

var query = new Parse.Query('YourClass');
query.notEqualTo('myBoolColumn', true);

query.find({...});
like image 31
victorMaje Avatar answered Sep 21 '22 04:09

victorMaje