Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GQL query where field is not null

I have an entity kind called Account. One of the fields is a String named selfie, which is basically the url to a selfie uploaded by a user. I want to fetch for users who have a selfie (so if a user does not have a selfie they should not be included in the result set). I have the following query. but it won't work because I have "NULL" as a string. What is the correct way for doing this? Again, I only want users who have selfies.

Filter selfie = new FilterPredicate("selfie", FilterOperator.GREATER_THAN, "NULL");
Query query = new Query(Account.class.getSimpleName()).setFilter(selfie);
FetchOptions options = FetchOptions.Builder.withLimit(30);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
QueryResultList<Entity> entities = datastore.prepare(query).asQueryResultList(options);

Also I am open to a JDO/JPA way of doing this on App-Engine (but it must work on App-Engine).

like image 644
Katedral Pillon Avatar asked Feb 17 '15 19:02

Katedral Pillon


2 Answers

After some trial and error on https://console.cloud.google.com/datastore/entities/query/gql

I found this work around expression for IS NOT NULL:

> NULL

Example:

Select * from State where customer_id > NULL

I haven't tested this on a field with negative values.

like image 133
Darian Hickman Avatar answered Oct 22 '22 04:10

Darian Hickman


This should be as simple as

Filter selfie = new FilterPredicate("selfie", FilterOperator.NOT_EQUAL, null);

where you actually pass the Java null value (not a String) and filter with not equals (see https://stackoverflow.com/a/22200137).

Personally, I've been using JDO for my datastore management, which has its pros and cons. Let me know if you'd like to see a solution using JDO as well. Cheers!

like image 23
Mr. Kevin Thomas Avatar answered Oct 22 '22 04:10

Mr. Kevin Thomas