Query Engine - Android SDK¶
On this page
To filter data in your realm, you can leverage Realm Database's query engine. Realm Database's query engine uses a Fluent interface to construct multi-clause queries. See RealmQuery API for a complete list of available methods.
There are several types of operators available to filter a
Filters work by evaluating an operator expression for
every object in the collection being
filtered. If the expression resolves to
Database includes the object in the results collection.
An expression consists of one of the following:
- The name of a property of the object currently being evaluated.
- An operator and up to two argument expression(s).
- A literal string, number, or date.
The examples in this page use a simple data set for a
task list app. The two Realm object types are
Task has a name, assignee's name, and
completed flag. There is also an arbitrary number for
priority -- higher is more important -- and a count of
minutes spent working on it. A
Project has zero or more
See the schema for these two classes,
The most straightforward operation in a search is to compare values.
The following example uses the query engine's comparison operators to:
- Find high priority tasks by comparing the value of the
priorityproperty value with a threshold number, above which priority can be considered high.
- Find just-started or short-running tasks by seeing if the
progressMinutesproperty falls within a certain range.
- Find unassigned tasks by finding tasks where the
assigneeproperty is equal to
- Find tasks assigned to specific teammates Ali or Jamie by seeing if the
assigneeproperty is in a list of names.
You can make compound predicates using logical operators.
Negates the result of the given expression.
We can use the query language's logical operators to find
all of Ali's completed tasks. That is, we find all tasks
assignee property value is equal to 'Ali' AND
isComplete property value is
You can compare string values using these string operators. Regex-like wildcards allow more flexibility in search.
For example, the wildcard string "d?g" matches "dog", "dig", and "dug", but not "ding", "dg", or "a dog".
We use the query engine's string operators to find projects with a name starting with the letter 'e' and projects with names that contain 'ie':
Case-insensitive string operators only support the
Latin Extended A, and
Latin Extended B (UTF-8 range 0–591) character sets. Setting
the case insensitive flag in queries when using
like only works on English locale characters.
You can apply an aggregate operator to a collection property of a Realm object. Aggregate operators traverse a collection and reduce it to a single value.
Evaluates to the average value of a given numerical property across a collection.
Evaluates to the number of objects in the given collection.
Evaluates to the highest value of a given numerical property across a collection.
Evaluates to the lowest value of a given numerical property across a collection.
Evaluates to the sum of a given numerical property across a collection.
We create a couple of filters to show different facets of the data:
- Projects with average tasks priority above 5.
- Long running projects.
Realm Query Language¶
You can also query realms using Realm Query Language, a language inspired by NSPredicate.
- There are several categories of operators available to filter results: comparison, logical, string and aggregate operators.