Filtering

Filtering

You can apply filters on any Create, Read, Update, and Delete data operations to alter the result of a query.

Types of Filtering Expressions

In the Backend Services Android SDK there are several Condition classes used to define filters:

  • ValueCondition - wraps the logic about comparing values.
  • ArrayCondition - whether or not an item is in array or all items in the array matches.
  • RegExCondition - wraps the logic about filtering with items by regular expression.

    Regular expressions support when filtering data from Data Connectors is limited to the following use cases. As a workaround, create a view or a stored procedure and map it to a Telerik Platform content type or cloud function respectively.

    • Starts with: ^searchstring
    • Ends with: searchstring$
    • Contains: .*searchstring.*

    Regular expression options are not supported when filtering data from Data Connectors. Do not specify any, otherwise you will receive an incorrect result.

  • CompoundCondition - allows you to chain conditions and apply "and" or "or" operators on them.

  • WhereCondition - uses the same syntax as the $where operator in MongoDB. This is theoretically the slowest filtering option, because it does not use database indexes.

Retrieving by Filter Expression

In Telerik Platform you can read items by single filter condition (ValueCondition) or several conditions (CompoundCondition):

Using ValueCondition

public void getBooksFromAuthor(EverliveApp app, String author) {
      RequestResult<ArrayList<Book>> booksFromAuthorResult;
        booksFromAuthorResult = app.workWith().data(Book.class).get().
            where(new ValueCondition("Author", author, ValueConditionOperator.EqualTo)).executeSync();
    if (booksFromAuthorResult.getSuccess()) {
        for (Book book : booksFromAuthorResult.getValue()) {
            Log.i("App_name", book.toString());
        }
    }
}

There are several ValueConditionOperators available:

  • EqualTo - Returns true if the value equals to the filter value.
  • NotEqualTo - Returns true if the value is not equal to the filter value.
  • GreaterThan - Returns true if the value is greater than the filter value.
  • GreaterThanOrEqualTo - Returns true if the value is greater or equal to the filter value.
  • LessThan - Returns true if the value is lesser than the filter value.
  • LessThanOrEqualTo - Returns true if the value is lesser or equal to the filter value.
  • IsOfType - Returns true if the value in the field is of the specified type.
  • ArrayIsOfSize - Used for array fields only. Returns true if the size of the array matches the specified number.

Deleting by Filter

You can delete filtered items by execute the following code snippet:

public void deleteMultiple(EverliveApp app, UUID userId) {
    app.workWith().data(Book.class).
        delete().
        where(new ValueCondition("UserId", userId, ValueConditionOperator.EqualTo).
        executeAsync();
}

Update by Filter

To update filtered items, you have to execute the following code snippet:

public void updateBooksByFilter(EverliveApp app, UUID userId) {
    Book newBook = new Book();
    newBook.setTitle("Changed title");
    app.workWith().data(Book.class).
       update(newBook).
       where(new ValueCondition("UserId", userId, ValueConditionOperator.EqualTo)).
       executeAsync();
}

Examples for Using Condition Classes

Here is an example of using Condition classes:

public ArrayList<Book> getRecentBookdByAuthors(EverliveApp app, List<UUID> userIds, Date date) {
    Condition containsCondition = new ArrayCondition("UserId", ArrayConditionOperator.ValueIsIn, userIds);
    Condition valueCondition = new ValueCondition("CreatedAt", date, ValueConditionOperator.GreaterThanOrEqualTo);
    Condition compoundCondition = new CompoundCondition(CompoundConditionOperator.And, Arrays.asList(containsCondition, valueCondition));

    RequestResult<ArrayList<Book>> requestResult = app.workWith().data(Book.class).get().where(compoundCondition).executeSync();
        return requestResult.getSuccess() ? requestResult.getValue() : null;
}
Start a free trial Request a demo
Contact us: +1-888-365-2779
sales@telerik.com
Copyright © 2016-2017, Progress Software Corporation and/or its subsidiaries or affiliates. All rights reserved.