Aug 29, 2013

GAE - Datastore filtering using IN operator

Since I can't find in datastore queries the documentation on how to use the IN operator, that why I decide to share my expirimentaion.

Here how I did.
Query q = new Query("YourKindHere");
// Create a list of possible data tobe compare in your IN operator
List<Long> levels =  new ArrayList<Long>(); // Initiate a List object
// Add the possible levels
// INFO: If you notice, I put "l" to represent the value as Long
arrayList.add(1l); // add level 1
arrayList.add(2l); // add level 2
arrayList.add(3l); // add level 3

// Create and AND operation with IN operators
q.setFilter(CompositeFilterOperator.and(
  FilterOperator.EQUAL.of("active", 1l), // compare as active=1
  FilterOperator.IN.of("level", levels) // compare ad level IN (1, 2, 3)
));


For other info about queries please check google appengine datastore query docs. Java DataStore Queries.

No comments: