Docs Menu
Docs Home
/ /
Atlas Device SDKs
/
/

Realm Query Language (RQL)

On this page

  • Examples on This Page
  • Query Syntax
  • Parameterized vs. Serialized Queries
  • Dot Notation
  • Nil Type
  • General Operators
  • Aggregate Operators
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Results Operators
  • Type-Specific Operators
  • Collection Operators
  • Dictionary Operators
  • String Operators
  • Type Operator
  • Type-Specific Queries
  • Date Queries
  • Full Text Search (FTS) Queries
  • Geospatial Queries
  • Inverse Relationship Queries (Backlinks)
  • List Comparison Queries
  • List Subqueries
  • ObjectId and UUID Queries
  • Device Sync Subscription Queries

Realm Query Language (RQL) is a string-based query language that you can use to construct queries for Atlas Device SDK. Each SDK provides language-specific filter or query methods that pass the queries to the Realm database query engine. For more information about SDK-specific query methods, refer to Read Objects.

Important

Swift SDK Does Not Support Realm Query Language (RQL)

The Swift SDK does not support querying with Realm Query Language. Instead, it uses NSPredicate to query a database. For more information, refer to Type-Safe and NSPredicate Queries (Swift SDK).

The following SDKs also support language-specific idiomatic APIs for querying databases:

In addition to using RQL in your client code, you can also use RQL in Realm Studio to browse for data.

Most of the examples on this page use a simple data set for a to-do list app that include the following database object types:

  • Item: Each item object has a name, assignee's name, and completed flag. There is also an arbitrary number for priority (where higher is more important) and a count of minutes spent working on it.

  • Project: Each project object has zero or more Items and an optional quota for minimum number of to-do items expected to be completed.

Realm Query Language syntax is based on NSPredicate.

Queries evaluate a predicate for every object in the collection being queried. If the predicate resolves to true, the results collection includes the object.

Realm Query Language uses expressions and predicates to filter objects. Filters consist of expressions within a predicate that evaluate to true or false.

Each expression contains the following elements:

  • Property name: The name of the object property to evaluate.

  • Operator and argument expression: A valid operator and up to two argument expressions. For example, in the expression A + B, the entirety of A + B is an expression, but A and B are also argument expressions to the operator +.

  • Value: The value, such as a string ('hello') or a number (5), to use in the evaluation.

"priority == 1"
// Property Name: priority
// Operator: ==
// Value: 1

Expressions can be combined with logical operators to create compound predicates.

Realm Query Language also supports the following special predicates:

  • TRUEPREDICATE - Always evaluates to true (returns all objects).

  • FALSEPREDICATE - Always evaluates to false (returns no objects).

You can format queries in serialized or parameterized format.

  • Serialized queries pass values directly in the expression.

    "progressMinutes > 1 AND assignee == 'Ali'"
  • Parameterized queries pass interpolated variables as additional arguments. The syntax for interpolated variables is $<int>, starting at 0 and incrementing for each additional variable.

    // Include one parameter with `$0`
    "progressMinutes > 1 AND assignee == $0", "Ali"
    // Include multiple parameters using ascending integers,
    // starting at`$0`
    "progressMinutes > $0 AND assignee == $1", 1, "Alex"

The following table shows how a query should be formatted when serialized and parameterized for the specific data types:

Type
Parameterized Example
Serialized Example
Note
Nil
"assignee == $0", null
"assignee == nil"
For parameterized queries, null maps to each SDK's respective null pointer.
Boolean
"setting == $0", false
"setting == false"
true or false values.
"name == $0", "George"
"name == 'George'"
Applies to string and char data type.
"age > $0", 5.50
"age > 5.50"
Applies to int, short, long, double, Decimal128, and float data types.
"date < $0", dateObject
"date < 2021-02-20@17:30:15:0"
For parameterized queries, you must pass in a date object. For serialized queries, the representation can be a supported Date format or a parameterized Date object.
"_id == $0", oidValue
"_id == oid(507f1f77bcf86cd799439011)"
For parameterized queries, you must pass in an ObjectId. For serialized queries, the string representation is oid(<ObjectId String>).
"id == $0", uuidValue
"id == uuid(d1b186e1-e9e0-4768-a1a7-c492519d47ee)"
For parameterized queries, you must pass in a UUID. For serialized queries, the string representation is uuid(<UUID String>).
Binary
"value == $0", "binary"
"value == 'binary'"
For ASCII characters, RQL serializes the binary value like a string, with quotes. For non-printable characters, RQL serializes the binary to a base 64 value.
"ANY items.name == {$0, $1}", "milk", "bread"
"ANY items.name == {'milk', 'bread'}"
Applies to collections, dictionaries, lists, and sets. Use a parameterized value for each member of the list.
RealmObject
"ANY items == $0", obj("Item", oid(6489f036f7bd0546377303ab))
"ANY items == obj('Item', oid(6489f036f7bd0546377303ab))"
To pass in a RealmObject, you need the class and primary key of the object.

You can use dot notation to refer to child properties of an object, including the properties of embedded objects and relationships:

// Find projects whose `items` list property contains an item
// with a specific name
"items[0].name == 'Approve project plan'"

You can also chain these notations for nested properties. For example, each Project has a projectLocation property that refers to an Office object, which itself contains an embedded object property address. You can chain dot notations to query the deeply nested zipcode property of an embedded address:

// Find projects whose `projectLocation` property contains an
// embedded Address object with a specific zip code
"projectLocation.address.zipcode == 10019"

Realm Query Language includes the nil type to represent a null pointer. You can either reference nil directly in your queries or with a parameterized query. If you're using a parameterized query, each SDK maps its respective null pointer to nil.

"assignee == nil"
"assignee == $0", null // 'null' maps to SDK language's null pointer

Use aggregate operators to traverse a collection and reduce it to a single value.

Operator
Description
@avg
Evaluates to the average value of a given numerical property across a collection. null values are ignored.
@count
Evaluates to the number of objects in the given collection.
@max
Evaluates to the highest value of a given numerical property across a collection. null values are ignored.
@min
Evaluates to the lowest value of a given numerical property across a collection. null values are ignored.
@sum
Evaluates to the sum of a given numerical property across a collection. null values are ignored.

The following example uses aggregate operators to find projects whose items collection property meets certain criteria:

var priorityNum = 5;
// Find projects with average item `priority` above 5
"items.@avg.priority > $0", priorityNum
// Find projects where every item has a `priority` less than 5
"items.@max.priority < $0", priorityNum
// Find projects where every item has `priority` greater than 5
"items.@min.priority > $0", priorityNum
// Find projects with more than 5 items
"items.@count > $0", 5
// Find projects where the sum total value of `progressMinutes`
// across all items is greater than 100
"items.@sum.progressMinutes > $0",
100

Use arithmetic operators to perform basic arithmetic in an expression when evaluating numeric data types, including object properties.

Operator
Description
*
Multiplication.
/
Division.
+
Addition.
-
Subtraction.
()
Group expressions together.

The following example uses arithmetic operators on Item object properties containing numeric values:

// Evaluate against an item's `priority` property value:
"2 * priority > 6" // (resolves to `priority > 3`)
"priority >= 2 * (2 - 1) + 2" // (resolves to `priority >= 4`)
// Evaluate against multiple object property values:
"progressMinutes * priority == 90"

Use comparison operators to compare values of matching data types. String values are compared lexicographically.

Operator
Description
BETWEEN {X, Y}
Evaluates to true if the left-hand expression (X) is between or equal to the right-hand expression (Y) range.
==, =
Evaluates to true if the left-hand expression is equal to the right-hand expression.
>
Evaluates to true if the left-hand expression is greater than the right-hand 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 expression is greater than or equal to the right-hand expression. For dates, this evaluates to true if the left-hand date is later than or the same as the right-hand date.
IN
Evaluates to true if the left-hand expression is in the right-hand list. This is equivalent to and used as a shorthand for == ANY collection operator.
<
Evaluates to true if the left-hand numerical or date expression is less than the right-hand numerical or date expression. For dates, this evaluates to true if the left-hand date is earlier than the right-hand date.
<=
Evaluates to true if the left-hand numeric expression is less than or equal to the right-hand numeric expression. For dates, this evaluates to true if the left-hand date is earlier than or the same as the right-hand date.
!=, <>
Evaluates to true if the left-hand expression is not equal to the right-hand expression.

The following example uses comparison operators to find to-do items whose property values match certain criteria:

// Compare `priority` values against a threshold value
"priority > $0", 5
// Compare `assignee` values to `null` value.
"assignee == $0", null
// Compare `priority` values against an inclusive range of values
"priority BETWEEN { $0 , $1 }", 1, 5
// Compare `progressMinutes` values against any of the listed values
"progressMinutes IN { $0, $1, $2 }", 10, 30, 60

Important

Types Must Match

The type on both sides of the operator must be equivalent. For example, comparing an ObjectId with string will result in a precondition failure with a message similar to the following:

"Expected object of type object id for property 'id' on object of type
'User', but received: 11223344556677889900aabb (Invalid value)"

You can compare any numeric type with any other numeric type, including decimal, float, and Decimal128.

Use logical operators to create compound predicates.

Operator
Description
AND
&&
Evaluates to true if both left-hand and right-hand expressions are true.
NOT
!
Negates the result of the given expression.
OR
||
Evaluates to true if either expression returns true.

The following example uses logical operators to combine multiple predicates:

// Find all items assigned to Ali AND marked completed
"assignee == $0 AND isComplete == $1", "Ali", true
// Find all items assigned to Alex OR to Ali
"assignee == $0 OR assignee == $1", "Alex", "Ali"

Use the sort, distinct, and limit operators to shape your query results collection. You can combine these operators in a single query across multiple properties. Operators are applied in the order they appear in the query.

Suffix
Description
SORT

Sort the results collection in the specified order for one or more comma-separated properties. The SDK applies the sort operation for each property in order, one at a time. Specify the sort order for each property as either:

  • ASC (ascending)

  • DESC (descending)

DISTINCT
Remove duplicates of the one or more comma-separated properties from the results collection. The SDK applies all distinct operations as a single AND condition, where duplicates must match all specified properties.
LIMIT
Limit the results collection to the specified number.

The following example finds all incomplete items, then shapes the returned results:

// Find incomplete items, sort by `priority` in descending order,
// then sort equal `priority` values by `progressMinutes`
// in ascending order
"isComplete == false SORT(priority DESC, progressMinutes ASC)"
// Find high priority items, then remove from the results any items
// with duplicate values for both `name` AND `assignee` properties
"priority >= 5 DISTINCT(name, assignee)"
// Find in-progress items, then return the first 10 results
"progressMinutes > 0 && isComplete != true LIMIT(10)"

Important

Order Matters

The SDK executes queries in order. This includes the order of any SORT, DISTINCT, and LIMIT operators in the query and the order of any properties within those operators. This can greatly impact the results returned. For example, sorting a query before limiting it can return very different results than sorting after limiting it.

// 1. Sorts by highest priority
// 2. Returns the first item
// 3. Remove duplicate names (N/A - a single item is always distinct)
"assignee == null SORT(priority ASC) LIMIT(1) DISTINCT(name)"
// 1. Removes any duplicates by name
// 2. Sorts by highest priority
// 3. Returns the first item
"assignee == null DISTINCT(name) SORT(priority ASC) LIMIT(1)"

A collection operator lets you query list properties within a collection of objects. Collection operators filter a collection by applying a predicate to every element of a given list property of the object. If the predicate returns true, the object is included in the output collection.

Operator
Description
ALL
Returns objects where the predicate evaluates to true for all objects in the collection.
ANY, SOME
Returns objects where the predicate evaluates to true for any objects in the collection. ANY is the default operator for list comparisons.
NONE
Returns objects where the predicate evaluates to false for all objects in the collection.

The following example uses collection operators to find projects whose items collection property meets certain criteria

// Find projects with no complete items
"NONE items.isComplete == $0", true
// Find projects that contain any item with priority 10
"items.priority == $0", 10 // (ANY operator is implied)
// Find projects that only contain completed items
"ALL items.isComplete == $0", true
// Find projects with at least one item assigned to
// either Alex or Ali
"ANY items.assignee IN { $0 , $1 }", "Alex", "Ali"
// Find projects with no items assigned to either Alex or Ali
"NONE items.assignee IN { $0 , $1 }", "Alex", "Ali"

Use dictionary operators in combination with comparison operators to filter objects based on dictionary keys and values.

Operator
Description
@values
Returns objects that have a specified dictionary value.
@keys
Returns objects that have a specified dictionary key.
@size, @count
The number of elements in a dictionary.
Dictionary['key']
The value of a dictionary key.
ALL | ANY | NONE <property>.@type
Checks if the dictionary contains properties of certain type.

The following example uses dictionary operators with comparison operators to find projects based on the comments dictionary property:

// Find projects whose `comments` dictionary property
// have a key of 'status'
"comments.@keys == $0", "status"
// Find projects whose `comments` dictionary property
// have a 'status' key with a value that ends in 'track'
"comments['status'] LIKE $0", "*track"
// Find projects whose `comments` dictionary property
// have more than one key-value pair
"comments.@count > $0", 1
// Find projects whose `comments` dictionary property contains
// only values of type 'string'
"ALL comments.@type == 'string'"
// Find projects whose `comments` dictionary property contains
// no values of type 'int'
"NONE comments.@type == 'int'"

Use string operators or comparison operators to filter objects based on string values. SDKs evaluate string values lexicographically.

Operator or Modifier
Description
BEGINSWITH
Evaluates to true if the left-hand string begins with the right-hand string. This is similar to contains, but only matches if the right-hand string is found at the beginning of the left-hand string.
CONTAINS
Evaluates to true if the right-hand string is found anywhere in the left-hand string.
ENDSWITH
Evaluates to true if the left-hand string ends with the right-hand string. This is similar to contains, but only matches if the left-hand string is found at the very end of the right-hand string.
LIKE

Evaluates to true if the left-hand string matches the right-hand string wildcard string. A wildcard string expression is a string that uses normal characters with two special wildcard characters:

  • The * wildcard matches zero or more of any character

  • The ? wildcard matches any character.

For example, the wildcard string "d?g" matches "dog", "dig", and "dug", but not "ding", "dg", or "a dog".

[c]
Modifier that indicates a string value is not case sensitive. String values are case sensitive by default.

The following example uses string and comparison operators to find projects based on the project name:

// Find projects whose name starts with 'E' or 'e'
// (case-insensitive query)
"name BEGINSWITH[c] $0", "E"
// Find projects whose name contains 'ie'
// (case-sensitive query)
"name CONTAINS $0", "ie"
// Find items where the assignee name is lexicographically between
// 'Ali' and 'Chris'(case-sensitive)
"assignee BETWEEN { $0 , $1 }", "Ali", "Chris"
// Find projects where the street address is lexicographically
// greater than '123 Main St'(case-sensitive)
"projectLocation.address.street > $0", "123 Main St"

Tip

String Values are Case Sensitive

String values are case sensitive by default. Use the [c] modifier to specify that values are not case sensitive.

For example, "name CONTAINS[c] $0", "da" would return "David" and "Ida", but "name CONTAINS $0", "da" would only return "Ida".

Note

Only Supported for Mixed Data Types and Dictionaries

You can currently only use the @type operator with mixed data types and dictionary properties.

Check the data type of a mixed type or dictionary property using the @type operator. Evaluate the property against a string representation of the data type name using string comparison operators. For information on how each SDK language's data types map to database-specific data types, refer to Define Property Types.

Operator
Description
@type

Check if type of a mixed type or dictionary property is the specified data type represented as a string.

Use == and != to compare equality.

The following example uses the @type operator to find projects whose mixed data type additionalInfo property type matches specific criteria:

// Find projects with an `additionalInfo` property of string type
"additionalInfo.@type == 'string'"
// Find projects with an `additionalInfo` property of
// `collection` type, which matches list or dictionary types
"additionalInfo.@type == 'collection'"
// Find projects with an `additionalInfo` property of list type,
// where any list element is of type 'bool'
"additionalInfo[*].@type == 'bool'"

Use comparison operators to query date types.

Specify dates in the following formats:

  • As a date in UTC, with the date and time separated by @ or T: YYYY-MM-DD@HH:mm:ss:nnnnnnnnnn (year-month-day@hours:minutes:seconds:nanoseconds), UTC.

  • As a time in seconds since the Unix epoch: Ts:n (T designates the start of the time, s is the number of seconds, and n is the number of nanoseconds).

var lastYear = new Date(1577883184000); // Unix timestamp in ms
var thisYear = new Date("2021-01-01@17:30:15:0"); // DateTime in UTC
var today = new Date("April 01, 2021 03:24:00"); // Alternate DateTime format

The following example uses a parameterized query to find items based on a new dateCreated property that populates when an item is marked as complete:

// Find to-do items completed before today's date
"dateCompleted < $0", today
// Find to-do items completed between start of the year to today
"dateCompleted > $0 AND dateCompleted < $1", thisYear, today

Tip

Parameterize Date Queries

We recommend using a parameterized query to pass the date data type from the SDK language you are using to your query.

Perform boolean match word searches to query on properties that have a full-text search (FTS) annotation. For information on enabling FTS on a property, refer to Enable Full-Text Search on a Property.

Operator
Description
TEXT

Evaluates to true if the left-hand FTS-enabled string property matches the right-hand string or wildcard string. A wildcard string expression is a string that uses normal characters with two special wildcard characters:

  • The * wildcard matches zero or more of any character that occur after the string.

  • The - wildcard matches all characters for exclusion from results.

Query strings are converted to tokens by a tokenizer using the following rules:

  • Tokens can only consist of characters from ASCII and the Latin-1 supplement western languages). All other characters are considered whitespace.

  • Strings split by a hyphen (-) are split into two tokens. For example, full-text splits into full and text.

  • Tokens are diacritics insensitive and case insensitive.

You can search for entire words or phrases, or limit your results with the following wildcard characters:

  • Exclude results for a word by placing the - character in front of the word.

  • Specify prefixes by placing the * wildcard at the end of a prefix. Suffix searching is not currently supported.

The following example uses the TEXT operator to find items based on their FTS-enabled name property:

// Find items with 'write' in the name.
"name TEXT $0", "write"
// Use '-' to exclude:
// Find items with 'write' but not 'tests' in the name
"name TEXT $0", "write -tests"
// Use '*' to match any characters after a prefix:
// Find items with a name that starts with 'wri'
"name TEXT $0", "wri*"

Use the geoWithin operator to query geospatial data. For more information on defining geospatial data, refer to Define a Geospatial Object.

Operator
Description
geoWithin

Returns objects where the latitude/longitude pair in a custom embedded object's coordinates property is contained within a specified geospatial shape.

The following geospatial shapes are supported:

  • GeoCircle

  • GeoBox

  • GeoPolygon

A geospatial query consists of:

  • An object with a property containing the embedded geospatial data.

  • A defined geospatial shape to set the boundary for the query:

    • GeoCircle

    • GeoBox

    • GeoPolygon

The query evaluates true if the coordinates of the embedded property are within the boundaries of the geospatial shape.

The following example compares the coordinates of the embedded location property against the boundaries of the GeoCircle shape named smallCircle:

"location geoWithin $0", smallCircle

Important

Geospatial Queries Do Not Support Parameterized Queries

You cannot use parameterized queries with geospatial data.

You can query an object's inverse relationship to another object through its backlink. Backlinks use the to-one and to-many relationships defined in your object schemas but reverse the direction. Every relationship that you define in your schema implicitly has a corresponding backlink.

Operator
Description
@links
Accesses the inverse relationship specified by the object type and property name (@links.<ObjectType>.<PropertyName>).
@count
The number of elements in a backlink collection. You can use the @count operator directly on @links to query the count of all relationships that point to an object.

You can access an inverse relationship:

  • Using the @links.<ObjectType>.<PropertyName> syntax, where <ObjectType> and <PropertyName> refer to a specific property on an object type that references the queried object type.

    // Find items that belong to a project with a quota less than 10
    // (using '@links.<ObjectType>.<PropertyName>')
    "@links.Project.items.quota < 10"
  • Using the linkingObjects property to explicitly include the backlink in your data model. This lets you reference the backlink through an assigned property name using dot notation.

    // Find items that belong to a project with a quota greater than 10
    // through the Item object's `projects` property
    // (using 'LinkingObjects')
    "projects.quota > 10"

The result is treated like a collection and supports collection operators and aggregate operators.

The following examples find items based on the projects they belong to through the @links operator or the projects property:

// Find items where no project that references the item has a
// quota greater than 10
"NONE @links.Project.items.quota > 10"
// Find items where all projects that reference the item have a
// quota less than 5
"ALL @links.Project.items.quota < 5"
// Find items that are referenced by multiple projects
"projects.@count > 1"
// Find items that are not referenced by any project
"@links.Project.items.@count == 0"
// Find items that belong to a project where the average item
// has been worked on for at least 10 minutes
"@links.Project.items.items.@avg.progressMinutes > 10"
// Find items that are not referenced by another object
// of any type (backlink count is 0)
"@links.@count == 0"

Use comparison operators and collection operators to filter based on lists of data. You can compare any type of valid list, including:

  • Collections of objects, which let you filter against other data in the database.

    // Find an item with the specified ObjectId value
    // in the`items` collection
    "oid(631a072f75120729dc9223d9) IN items._id"
  • Lists defined directly in the query, which let you filter against static data. You define static lists as a comma-separated list of literal values enclosed in opening ({) and closing (}) braces.

    // Find items with a priority value matching any value
    // in the static list
    "priority IN {0, 1, 2}"
  • Native list objects passed in a parameterized expression, which let you pass application data directly to your queries.

    const ids = [
    new BSON.ObjectId("631a072f75120729dc9223d9"),
    new BSON.ObjectId("631a0737c98f89f5b81cd24d"),
    new BSON.ObjectId("631a073c833a34ade21db2b2"),
    ];
    // Find items with an ObjectId value matching any value
    // in the parameterized list
    const parameterizedQuery = realm.objects(Item).filtered("_id IN $0", ids);

Note

Defaults to ANY

If you do not define a collection operator, a list expression defaults to the ANY operator.

"assignee == ANY { $0, $1 }", "Alex", "Ali"
"assignee == { $0, $1 }", "Alex", "Ali" // Equivalent (ANY is implied)

The following table demonstrates how collection operators interact with lists and comparison operators:

Expression
Match?
Reason
ANY {1, 2, 3} > ALL {1, 2}
true
A value on the left (3) is greater than some value on the right (both 1 and 2)
ANY {1, 2, 3} == NONE {1, 2}
true
3 does not match either of 1 or 2
ANY {4, 8} == ANY {5, 9, 11}
false
Neither 4 nor 8 matches any value on the right (5, 9 or 11)
ANY {1, 2, 7} <= NONE {1, 2}
true
A value on the left (7) is not less than or equal to both 1 and 2
ALL {1, 2} IN ANY {1, 2, 3}
true
Every value on the left (1 and 2) is equal to 1, 2 or 3
ALL {3, 1, 4, 3} == NONE {1, 2}
false
1 matches a value in the NONE list (1 or 2)
ALL {} in ALL {1, 2}
true
An empty list matches all lists
NONE {1, 2, 3, 12} > ALL {5, 9, 11}
false
12 is bigger than all values on the right (5, 9, and 11)
NONE {4, 8} > ALL {5, 9, 11}
true
4 and 8 are both less than some value on the right (5, 9, or 11)
NONE {0, 1} < NONE {1, 2}
true
0 and 1 are both less than none of 1 and 2

Use the SUBQUERY() predicate function to iterate through list properties with an additional query. Subqueries are useful when you need to match objects in a list property based on multiple conditions.

Subqueries use the following syntax:

SUBQUERY(<collection>, $<variableName>, <predicate>).@count

Operator
Description
SUBQUERY()

Returns list objects for the specified collection where the predicate evaluates to true. Contains the following parameters:

  • collection: The name of the list property to iterate through. This must be a list of objects, not a list of primitive types.

  • variableName: A variable name of the element to use in the subquery, prefixed with $.

  • predicate: The subquery predicate. Use the variable specified by variableName to refer to the currently-iterated element.

@count
The number of objects in the subquery results collection. This is required for subquery filters. You can use the count of the subquery result as you would any other number in a valid expression.

The following example uses subquery filters to find projects based on the items collection property using the $item variable name:

// Find projects with incomplete items with 'Demo' in the name
"SUBQUERY(items, $item, $item.isComplete == false AND $item.name CONTAINS[c] 'Demo').@count > 0"
// Find projects where the number of completed items is
// greater than or equal to the project's `quota` property
"SUBQUERY(items, $item, $item.isComplete == true).@count >= quota"

Tip

Compare Count to 0 to Return All Matching Objects

The @count aggregate operator returns the number of objects in the subquery results collection. You can compare the count with the number 0 to return all matching objects.

Use comparison operators to compare BSON ObjectIds and UUIDs for equality. These data types are often used as primary keys.

You can use pass ObjectId and UUID values either as parameterized query arguments or to the oid() or uuid() predicate functions, respectively.

Operator
Description
oid(<ObjectId String>)
The string representation of the ObjectID to evaluate.
uuid(<UUID String>)
The string representation of the UUID to evaluate.

The following examples use equality comparison operators to find items based on their _id property:

// Find an item whose `_id` matches the ObjectID value
// passed to 'oid()'
"_id == oid(6001c033600510df3bbfd864)"
// Find an item whose `_id` matches the ObjectID passed as
// a parameterized query argument
"_id == $0", oidValue
// Find an item whose `id` matches the UUID value
// passed to 'uuid()'
"id == uuid(d1b186e1-e9e0-4768-a1a7-c492519d47ee)"
// Find an item whose `_id` matches the UUID passed as
// a parameterized query argument
"id == $0", uuidValue

If your app uses Atlas Device Sync, you can use Realm Query Language to construct your sync subscription queries. However, App Services does not support all RQL operators or functionality in a subscription query.

Device Sync does not support the following in a subscription query:

However, you can use the following:

  • You can use @count for array fields.

  • You can query lists using the IN operator:

    // Query a constant list for a queryable field value
    "priority IN { 1, 2, 3 }"
    // Query an array-valued queryable field for a constant value
    "'comedy' IN genres"

Tip

Case-Insensitive Queries Not Recommended

Although case-insensitive string queries (using the [c] modifier) are supported in sync queries, they do not use indexes effectively and can lead to performance problems. As a result, they are not recommended.

Additionally, note the following if you are using an indexed queryable field in your app:

  • Every query must include the indexed queryable field.

  • Every query must directly compare the indexed queryable field against a constant using an == or IN operator at least once.

  • You can optionally include an AND comparison as well.

← 
 →