Para filtrar datos en tu realm, puedes aprovechar el motor de query de Realm. También puedes ordenar los datos filtrados. Para un ejemplo de cómo ordenar resultados de consultas, consulta Ordenar listas y resultados de consultas.
Nota
The C++ SDK does not yet support the full range of query expressions that the other SDKs provide.
About the Examples on This Page
Los ejemplos de esta página utilizan un conjunto de datos simple para una aplicación de lista de tareas pendientes. Los dos tipos de objetos Realm son Project
y Item. Un Item tiene:
A name
A completed flag
An optional assignee's name
A number repesenting priority, where higher is more important
A count of minutes spent working on it
A Project has a name and a to-many relationship to zero or more Items.
The schemas for Project and Item are:
struct Item { std::string name; bool isComplete; std::optional<std::string> assignee; int64_t priority; int64_t progressMinutes; }; REALM_SCHEMA(Item, name, isComplete, assignee, priority, progressMinutes) struct Project { std::string name; std::vector<Item*> items; }; REALM_SCHEMA(Project, name, items)
You can set up the realm for these examples with the following code:
auto config = realm::db_config(); auto realm = realm::db(std::move(config)); auto item1 = realm::Item{.name = "Save the cheerleader", .assignee = std::string("Peter"), .isComplete = false, .priority = 6, .progressMinutes = 30}; auto project = realm::Project{.name = "New project"}; project.items.push_back(&item1); realm.write([&] { realm.add(std::move(project)); }); auto items = realm.objects<realm::Item>(); auto projects = realm.objects<realm::Project>();
Filtro de datos
Operadores de comparación
Comparaciones de valores
Operador | Descripción |
|---|---|
== | Evaluates to |
> | Evaluates to |
>= | Evaluates to |
< | Devuelve |
<= | Evaluates to |
!= | Evaluates to |
Ejemplo
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 tostd::nullopt.Find tasks assigned to specific teammates Ali or Jamie by seeing if the
assigneeproperty is in a list of names.
auto highPriorityItems = items.where([](auto const& item) { return item.priority > 5; }); auto quickItems = items.where([](auto const& item) { return item.progressMinutes > 1 && item.progressMinutes < 30; }); auto unassignedItems = items.where( [](auto const& item) { return item.assignee == std::nullopt; }); auto aliOrJamieItems = items.where([](auto const& item) { return item.assignee == std::string("Ali") || item.assignee == std::string("Jamie"); });
Operadores lógicos
You can use the logical operators listed in the following table to make compound predicates:
Operador | Descripción |
|---|---|
&& | Evaluates to |
! | Negates the result of the given expression. |
|| | Evaluates to |
Ejemplo
We can use the query language's logical operators to find all of Ali's completed tasks. That is, we find all tasks where the assignee property value is equal to 'Ali' AND the isComplete property value is true:
auto completedItemsForAli = items.where([](auto const& item) { return item.assignee == std::string("Ali") && item.isComplete == true; });
Operadores de String
You can compare string values using these string operators.
Operador | Descripción |
|---|---|
.contains(_ value: String) | Evalúa como |
== | Evaluates to |
!= | Evaluates to |
Ejemplo
The following example uses the query engine's string operators to find:
Projects with names that contain 'ie'
auto containIe = items.where([](auto const& item) { return item.name.contains("ie"); });