Search code examples
javagoogle-app-enginegoogle-cloud-datastore

How to filter whether datastore property is present in list or not


This query is written in JDO in which I am filtering users based on their country. Contacts Kind has a property country. Only those users whose country is present in the below list are fetched.

List<String> countryList = Arrays.asList("USA","India","France");



SELECT FROM Contacts WHERE :countryList.contains(country) && loginType == 'Google' && dateAdded >= 0 && dateAdded <= 1567078411000 ORDER BY dateAdded DESC RANGE 0,30

How to write the code for the above query in Low-level Datastore ?


Solution

  • Although it is not the recommended solution, as it will need to be upgraded to the recommended APIs before migrating to newer App Engine runtimes, you can do these sort of “WHERE IN” queries using the Java App Engine API client. Like in the example below:

    List<String> countryList = Arrays.asList("USA","India","France");
    
    Filter propertyFilter =
            new FilterPredicate("height", FilterOperator.IN, countryList);
    Query q = new Query("Contacts").setFilter(propertyFilter);
    

    This is not supported in the new Datastore Client Library for Java, however, and I believe it is because, under the hood, Datastore translates this query into individual queries for each element on the list. Like explained in this post.