Query operators and criteria
The query operators that filter Table and Aggregate queries in server scripts, with their aliases and how to combine them.
Query operators build the criteria that filter a Table or Aggregate query. Use this page to look up an operator’s arguments and aliases while you write a server script.
How to use the operators
An operator is used in one of two ways:
- Chained on a Table or Aggregate, it adds a criterion to the query and returns the Table or Aggregate. Chained operators combine with AND.
- Called on its own, it returns a criterion. You pass that criterion to
AND,OR,NOT,Table(tableName, criterion),recordExists(criterion),addCriteria(criterion)ormatchesCriterion(criterion).
// Chained
Table("User").EQUAL("Active", true).STARTS_WITH("UserName", "a");
// On its own, inside a logical operator
Table("User").OR(EMPTY("LastName"), EQUAL("Active", false));
Operator names are written in capitals. Several have a shorter alias, given in the description. The first argument is always a field name, except for the logical operators.
Comparison operators
| Method | Returns | Description | Since |
|---|---|---|---|
EQUAL(field, value) |
Criterion | Matches records where the field equals the value. Alias EQ. |
1.11 |
NOT_EQUAL(field, value) |
Criterion | Matches records where the field does not equal the value. Alias NE. |
1.11 |
NOT_EQUAL_OR_IS_EMPTY(field, value) |
Criterion | Matches records where the field does not equal the value or is empty. | 1.11 |
GT(field, value) |
Criterion | Greater than the value. | 1.11 |
GTE(field, value) |
Criterion | Greater than or equal to the value. Alias GE. |
1.11 |
LT(field, value) |
Criterion | Less than the value. | 1.11 |
LTE(field, value) |
Criterion | Less than or equal to the value. Alias LE. |
1.11 |
BETWEEN(field, from, to) |
Criterion | Matches values from from to to. |
1.11 |
IN(field, values) |
Criterion | Matches records where the field equals one of the values in the array. | 1.11 |
NOT_IN(field, values) |
Criterion | Matches records where the field equals none of the values. Alias NOTIN. |
1.11 |
TRUE(field) |
Criterion | Matches records where a Boolean field is true. |
1.11 |
FALSE(field) |
Criterion | Matches records where a Boolean field is false. |
1.11 |
EMPTY(field) |
Criterion | Matches records where the field has no value. | 1.11 |
NOT_EMPTY(field) |
Criterion | Matches records where the field has a value. Alias NOTEMPTY. |
1.11 |
On a relation that holds many records, such as a user’s Group, IN and NOT_IN take record IDs, and EMPTY and NOT_EMPTY test whether there are any related records.
Text operators
| Method | Returns | Description | Since |
|---|---|---|---|
LIKE(field, text) |
Criterion | Matches values that contain the text. Text of one or two characters matches values that start with it. | 1.11 |
CONTAINS(field, text) |
Criterion | Matches values that contain the text. | 1.11 |
DOES_NOT_CONTAIN(field, text) |
Criterion | Matches values that do not contain the text. | 1.11 |
STARTS_WITH(field, text) |
Criterion | Matches values that start with the text. Alias STARTSWITH. |
1.11 |
LIKE, CONTAINS and DOES_NOT_CONTAIN treat % and _ as ordinary characters, so you cannot pass your own wildcards. They are case-sensitive unless the field has a trigram index.
Logical and sub-query operators
| Method | Returns | Description | Since |
|---|---|---|---|
AND(criterion, ...) |
Criterion | Matches records that meet every criterion. | 1.11 |
OR(criterion, ...) |
Criterion | Matches records that meet at least one criterion. | 1.11 |
NOT(criterion) |
Criterion | Matches records that do not meet the criterion. | 1.11 |
SUBQUERY(relation, criterion) |
Criterion | Matches records with a related record, through the named relation, that meets the criterion. | 1.11 |
SUBQUERY(targetTable, targetField, sourceField, criterion) |
Criterion | Matches records whose sourceField value appears in targetField of the target table’s records that meet the criterion. Use it for tables with no relation between them. |
1.11 |
Ordering and network operators
| Method | Returns | Description | Since |
|---|---|---|---|
ORDER_BY(field) |
Criterion | Sorts the results by the field, ascending. Alias ASC. |
1.11 |
ORDER_BY_DESC(field) |
Criterion | Sorts the results by the field, descending. Alias DESC. |
1.11 |
INET_CONTAINS(field, address) |
Criterion | Matches network fields whose range contains the address. | 1.11 |
INET_CONTAINS_OR_EQUALS(field, address) |
Criterion | Same, also matching an equal range. | 1.11 |
INET_CONTAINED_WITHIN(field, network) |
Criterion | Matches network fields that lie within the network. | 1.11 |
INET_CONTAINED_WITHIN_OR_EQUALS(field, network) |
Criterion | Same, also matching an equal range. | 1.11 |
To reuse a filter built on a list, copy it as JSON and pass it to addJsonQuery. Query records and operators describes the JSON form.
Examples
Combine operators
Indent nested operators so the logic stays readable. This query finds users with no last name, or users whose user name starts with a and whose last name is set, or three named users.
Table("User")
.OR(
EMPTY("LastName"),
AND(
STARTS_WITH("UserName", "a"),
NOT_EMPTY("LastName")
),
IN("UserName", ["<user name>", "<user name>", "<user name>"])
);
Match a date range
Servicely Script
Table("User")
.BETWEEN("BirthDate", DateTime().withDate(1960, 1, 1), DateTime())
.resultCount();
Response
// The number of users born from 1960 to today
12
Query through a relation
This query returns the active users who belong to a group whose name starts with Support.
Table("User")
.EQUAL("Active", true)
.SUBQUERY("Group", STARTS_WITH("Name", "Support"))
.each(user => log.info(user.UserName()));
Query a table with no relation
AvailableValue and LocalizedMessage have no relation. This query matches choice values whose English message starts with New.
Table("AvailableValue")
.EQUAL("Group", "StateProvince")
.SUBQUERY(
"LocalizedMessage", // target table
"I18nKey", // field in the target table
"Value", // field in AvailableValue to match
AND(
STARTS_WITH("Message", "New"),
EQUAL("Locale", "en_US")
)
)
.each(rec => log.info(rec.Key() + ": " + rec.Value()));