5

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).

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199

2 Answers2

5

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.

Darian Hickman
  • 853
  • 10
  • 26
1

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!

Community
  • 1
  • 1
Mr. Kevin Thomas
  • 837
  • 2
  • 13
  • 21