danielz
danielz

Reputation: 1767

Google Cloud Datastore get entities that have a boolean set to false or null?

I have a bunch of entities with a boolean field that can either be: true, false, or null (the actual null value). I need to retrieve all the entities that have this field set to null or false. I know that that GCD doesn't support a not equal query but I think I got it to work by querying for all the entities that have the field less than true. It seems to work but I am not sure why it works. Are null and false always less than true?

Upvotes: 0

Views: 944

Answers (2)

Artsiom Miksiuk
Artsiom Miksiuk

Reputation: 4303

Datastore not supports OR and != (not equal) operators natively.

Python implementations, of != is fully programmatic and have number of limitation when used with another query operations.

Search with IN operator when [False, None] provided will cause multiple query to be launched and merged in a single result with this call.

The '< True' operation will not find properties without any value (empty val) as it is not indexed, but will find false and null values and this can be done in a single query.

So, the best option will be (on JavaScript):

let query = db.createQuery("SomeNamespace", "SomeKind")
    .filter("property", "<", true);

Upvotes: 0

GAEfan
GAEfan

Reputation: 11360

Any of these work:

test_query = Test.query(Test.bool != True).fetch()
test_query = Test.query(Test.bool.IN([False, None])).fetch()
test_query = Test.query(Test.bool < True).fetch()

In Python:

>>> None < False < True
True

Upvotes: 1

Related Questions