A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/node/crud/query-data/ below:

Query Data - Node.js SDK - Atlas Device SDKs

To filter data in your Realms, construct queries with Realm Query Language. Queries always reflect the latest state of an object and emit notifications that can update your app whenever data changes.

For more information about syntax, usage and limitations, refer to the Realm Query Language reference.

A results collection represents all objects in a realm that match a query operation. In general you can work with a collection like a regular JavaScript array but collections don't actually hold matching Realm objects in memory. Instead they reference the matched objects, which themselves map directly to data in the realm file.

Note Pagination & Limits

Some queries only need to access a subset of all objects that match the query. Realm's lazy-loaded collections only fetch objects when you actually access them, so you do not need any special mechanism to limit query results.

For example, if you only want to find 10 matching objects at a time (such as in a paged product catalog) you can just access ten elements of the results collection. To advance to the next page, access the next ten elements of the results collection starting at the index immediately following the last element of the previous page.

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().

const items = realm.objects("Item");const importantItems = items.filtered("priority >= $0", 7);

You can use Realm Query Language (RQL) to query on properties that have a Full-Text Search (FTS) index. To query an FTS indexed property, use the TEXT predicate in your filtered query.

Exclude results for a word by placing the hyphen (-) character in front of the word. For example, a search for swan -lake would include all search results for swan excluding those with lake.

In the following example, we query on the Book.name field using the following Book object model:

class Book extends Realm.Object<Book> {  name!: string;  price?: number;  static schema: ObjectSchema = {    name: "Book",    properties: {      name: { type: "string", indexed: "full-text" },      price: "int?",    },  };}
const books = realm.objects(Book);const booksWithHunger = books.filtered("name TEXT $0", "hunger");const booksWithSwanWithoutLake = books.filtered("name TEXT $0", "swan -lake");

Full-Text Search (FTS) indexes support:

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 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:

const highPriorityTasks = tasks.filtered("priority > $0", 5);const unassignedTasks = tasks.filtered("assignee == $0", null);const lowProgressTasks = tasks.filtered("$0 <= progressMinutes && progressMinutes < $1", 1, 10);const aliTasks = tasks.filtered("assignee == $0", "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 == $0 && isComplete == $1", "Ali", 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'.

console.log(  "Projects that start with 'e': " +    projects.filtered("name BEGINSWITH[c] $0", 'e').length);console.log(  "Projects that contain 'ie': " +    projects.filtered("name CONTAINS $0", 'ie').length);

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:

console.log(  "Number of projects with average tasks priority above 5: " +    projects.filtered("tasks.@avg.priority > $0", 5).length);console.log(  "Number of long-running projects: " +    projects.filtered("tasks.@sum.progressMinutes > $0", 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:

console.log(  "Number of projects with no complete tasks: " +    projects.filtered("ALL tasks.isComplete == $0", false).length);console.log(  "Number of projects with any top priority tasks: " +    projects.filtered("ANY tasks.priority == $0", 10).length);

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4