Docs Menu

Query Data - React Native SDK

On this page

  • Examples on This Page
  • Construct a Query
  • Operators
  • Comparison Operators
  • Logical Operators
  • String Operators
  • Aggregate Operators
  • Collection Operators
  • Summary

To filter data in your Realms, construct queries with Realm Query Language. For more information about syntax, usage and limitations, refer to the Realm Query Language reference.

The examples in this page use a simple data set for a task list app. The two Realm object types are Project and Task. A 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 Tasks.

See the schema for these two classes, Project and Task:

const TaskSchema = {
name: "Task",
properties: {
name: "string",
isComplete: "bool",
priority: "int",
progressMinutes: "int",
assignee: "string?"
}
};
const ProjectSchema = {
name: "Project",
properties: {
name: "string",
tasks: "Task[]"
}
};

To filter data, pass a query made with Realm Query Language to Realm.Results.filtered()

TODO: literal include

const tasks = realm.objects('Task');
// Gets all tasks where the 'priority' property is 7 or more.
const importantTasks = tasks.filtered("priority >= 7");

There are several types of operators available to filter a Realm collection. Filters work by evaluating an operator expression for every object in the collection being filtered. If the expression resolves to true, Realm 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 most straightforward operation in a search is to compare values. Realm Query Language has standard comparison operators like ==, >, >=, in, <, <=, and !=.

For complete documentation on comparison operators, refer to the Realm Query Language comparison operator reference.

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

  • Find high priority tasks by comparing the value of the priority property value with a threshold number, above which priority can be considered high.
  • 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 null.
  • Find tasks assigned to specific teammates Ali or Jamie by seeing if the assignee property is in a list of names.
const highPriorityTasks = tasks.filtered("priority > 5");
const unassignedTasks = tasks.filtered("assignee == null");
const lowProgressTasks = tasks.filtered("1 <= progressMinutes && progressMinutes < 10");
const aliTasks = tasks.filtered("assignee == 'Ali'");
console.log(
`Number of high priority tasks: ${highPriorityTasks.length}`,
`Number of unassigned tasks: ${unassignedTasks.length}`,
`Number of just-started or short-running tasks: ${lowProgressTasks.length}`,
`Number of tasks for Ali: ${aliTasks.length}`
);

Create compound predicates using logical operators. Realm Query Language has standard logical operators like AND, OR, and NOT.

For complete documentation on logical operators, refer to the Realm Query Language logical operator reference.

The following example uses Realm Query Language's logical operators to find all of Ali's completed tasks. We find all tasks where the assignee property value is equal to 'Ali' AND the isComplete property value is true.

console.log(
"Number of Ali's complete tasks: " +
tasks.filtered("assignee == 'Ali' && isComplete == true").length
);

You can compare string values using string operators like ==, beginsWith, contains, and endsWith. You can also use the LIKE operator to search with regex-like wildcards.

For complete documentation on string operators, refer to the Realm Query Language string operator reference.

The following example uses Realm Query Language's string operators to find projects with a name starting with the letter 'e' and projects with names that contain 'ie'.

// Use [c] for case-insensitivity.
console.log(
"Projects that start with 'e': " +
projects.filtered("name BEGINSWITH[c] 'e'").length
);
console.log(
"Projects that contain 'ie': " +
projects.filtered("name CONTAINS 'ie'").length
);

TODO: resume here

Traverse a collection and reduce it to a single value with an aggregate operator.

For complete documentation on aggregate operators, refer to the Realm Query Language aggregate operator reference.

The following examples uses aggregate operators to show different facets of the data:

  • @avg to show projects with average tasks priority above 5.
  • @sum to show long running projects.
console.log(
"Number of projects with average tasks priority above 5: " +
projects.filtered("tasks.@avg.priority > 5").length
);
console.log(
"Number of long-running projects: " +
projects.filtered("tasks.@sum.progressMinutes > 120").length
);

A collection operator uses rules to determine whether to pass each input collection object to the output collection by applying a given predicate to every element of a given list property of the object.

For complete documentation on collection operators, refer to the Realm Query Language collection operator reference.

The following examples uses Realm Query Language's collection operators to find:

  • ALL for projects with no complete tasks.
  • ANY for projects with any top priority tasks.
console.log(
"Number of projects with no complete tasks: " +
projects.filtered("ALL tasks.isComplete == false").length
);
console.log(
"Number of projects with any top priority tasks: " +
projects.filtered("ANY tasks.priority == 10").length
);
  • Use Realm.Results.filtered to filter data in your realm using Realm Query Language.
  • For a detailed explanation of Realm Query Language, refer to the Realm Query Language reference.
  • There are several categories of operators available to filter results: - comparison - logical - string - aggregate - set
←  Use Change Listeners In Components - React Native SDKModify an Object Schema - React Native SDK →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.