Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs Menu
Docs Home
/ /
CRUD - C++ SDK

Filter Data - C++ SDK

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.

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

Un Project tiene un nombre y una relación de muchos con cero o más 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>();

Comparaciones de valores

Operador
Descripción
==

Evaluates to true if the left-hand expression is equal to the right-hand expression.

>

Evaluates to true if the left-hand numerical or date expression is greater than the right-hand numerical or date expression. For dates, this evaluates to true if the left-hand date is later than the right-hand date.

>=

Evaluates to true if the left-hand numerical or date expression is greater than or equal to the right-hand numerical or date expression. For dates, this evaluates to true if the left-hand date is later than or the same as the right-hand date.

<

Devuelve true si la expresión numérica o de fecha de la izquierda es menor que la expresión numérica o de fecha de la derecha. Para fechas, esto da como resultado true si la fecha de la izquierda es anterior a la fecha de la derecha.

<=

Se evalúa como true si la expresión numérica de la izquierda es menor o igual que la de la derecha. Para las fechas, se evalúa como true si la fecha de la izquierda es anterior o igual a la de la derecha.

!=

Evaluates to true if the left-hand expression is not equal to the right-hand expression.

Ejemplo

The following example uses the query engine's comparison operators to:

  • Encuentre tareas de alta prioridad comparando el valor de la propiedad priority con un número umbral, por encima del cual la prioridad puede considerarse alta.

  • Find just-started or short-running tasks by seeing if the progressMinutes property falls within a certain range.

  • Find unassigned tasks by finding tasks where the assignee property is equal to std::nullopt.

  • Find tasks assigned to specific teammates Ali or Jamie by seeing if the assignee property 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");
});

You can use the logical operators listed in the following table to make compound predicates:

Operador
Descripción
&&

Se evalúa como true si las expresiones de la izquierda y la derecha son true.

!

Negates the result of the given expression.

||

Evaluates to true if either expression returns true.

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;
});

You can compare string values using these string operators.

Operador
Descripción
.contains(_ value: String)

Evalúa como true si la expresión de string a la izquierda se encuentra en cualquier parte de la expresión de string a la derecha.

==

Evaluates to true if the left-hand string is lexicographically equal to the right-hand string.

!=

Se evalúa como true si la cadena de la izquierda no es lexicográficamente igual a la cadena de la derecha.

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"); });

Volver

Borrar

En esta página