1. Query model
UnQuery defines an abstract filter: a tree of conditions over the fields of a document. The JSON syntax (section 2) and the SQL syntax (section 3) are two ways of writing the same filter. Implementations translate the filter to each data source; neither syntax is passed to a data source as text.
1.1 Filters and conditions
Section titled “1.1 Filters and conditions”A filter is one of:
- a condition: a field path, an operator and an operand, such as
agegte18; - a logical combination:
andororover a list of filters,norover a list of filters, ornotover a single filter.
MODEL_TRANSLATE MUST
An implementation MUST translate every filter, in any syntax, into the abstract filter model before evaluating it or translating it to a data source.
1.2 Field paths
Section titled “1.2 Field paths”A field path is a sequence of segments separated by ., one segment per nesting level. Each segment is a field name, written either plain or between double quotes, optionally followed by array indexes.
path = segment *( "." segment )segment = name *indexname = plain / quotedplain = 1*( %x20-21 / %x23-2D / %x2F-5A / %x5C / %x5E-D7FF / %xE000-10FFFF ) ; any Unicode scalar value from U+0020, except ".", DQUOTE, "[" and "]"quoted = DQUOTE 1*( %x20-21 / %x23-D7FF / %xE000-10FFFF / DQUOTE DQUOTE ) DQUOTE ; any Unicode scalar value from U+0020index = "[" ( "0" / %x31-39 *DIGIT ) "]" ; zero-based, no leading zerosPATH_DOT_NOTATION MUST
A field path MUST follow the path grammar above. A plain name may contain any character from U+0020 except ., ", [ and ]; a quoted name may contain any character from U+0020, and represents a " as "". Control characters (U+0000 to U+001F) are not allowed in names.
PATH_RESERVED_START MUST NOT
A field name (the name of a segment, after removing the quotes of a quoted name, without its indexes) MUST NOT start with $ or @, which are reserved in the LingoData family for ECSON typed values and for operators and control parameters.
PATH_INDEX MUST
An index [n], after a field name or after another index, MUST select the element at the zero-based position n of the array at that point of the path. When the value at that point is not an array, or the array has no element at that position, the field MUST be treated as missing.
PATH_NUMERIC_SEGMENT MUST
A segment made only of digits, such as 0 in items.0.price, MUST be read as a field name, never as an array index.
PATH_ARRAYS MUST
When a segment of a field path resolves to an array and has no index, the rest of the path MUST be resolved in each element of the array, and the condition MUST match if it matches the value found in at least one element.
PATH_ARRAY_NESTED MUST
When the rest of the path is resolved in the elements of an array (PATH_ARRAYS), an element that is not a plain object, or that does not have the next field, MUST count as missing for that element. Arrays nested inside arrays MUST NOT be flattened.
PATH_INDEX_AS_IS MUST
A segment with an index MUST use the selected element as it is: when that element is an array, the next segment MUST NOT be resolved in its elements.
PATH_ARRAY_VALUE MUST
When the value at the end of the path is an array, a condition MUST match if it matches the whole array or at least one of its elements. This rule does not apply to the operators of the Array set (section 1.5.2), which evaluate only the value at the end of the path.
The complements ne, nin, not and nor MUST match exactly when the condition they negate does not match (NULL_NE). The array rules of this section apply to the negated condition, never to the complement itself: items.price with nin [5] does not match { "items": [{ "price": 5 }, { "price": 6 }] }, because in [5] does.
PATH_ARRAY_EMPTY MUST
A path that continues past an empty array, such as items.price when items is [], has no value and MUST be evaluated as a missing field (section 1.3): exists with operand false and eq with operand null match, and conditions such as gt or exists with operand true do not. An empty array at the end of the path is a present value.
A condition on a field path MUST be evaluated independently of the other conditions on the same path, so different conditions MAY be satisfied by different elements of an array.
1.3 Missing fields, null and types
Section titled “1.3 Missing fields, null and types”Filters use two-valued logic: a condition either matches a document or does not, and the result of the rules in this section never depends on the data source. Two optional sets are the exceptions: @regex follows the dialect of each data source (section 1.5.3), and geospatial results may differ near the edges of large polygons (section 1.5.1).
NULL_EQ MUST
eq with operand null MUST match documents where the field is null or missing.
NULL_EXISTS MUST
exists with operand false MUST match only documents where the field is missing; with operand true, only documents where the field is present, including when its value is null.
NULL_UNDEFINED MUST
A field whose value is undefined (ECSON $undefined) MUST be treated as missing.
eq with the operand undefined MUST match exactly the documents where the field is missing (NULL_UNDEFINED), and MUST NOT match a field whose value is null or an array with an undefined element (NULL_UNDEFINED_ELEMENT). On a path through an array, it follows PATH_ARRAYS, like eq null: it matches when the field is missing in at least one element. in with undefined in its list MUST match missing fields too.
NULL_UNDEFINED_IN_OPERAND MUST
An undefined inside an array, object, $set or $map operand, such as eq [1, undefined], MUST make the filter fail, except when it is itself an element of the list of in or nin.
An undefined element inside an array of a document MUST be treated as null. This rule does not apply to operands (NULL_UNDEFINED_IN_OPERAND).
NULL_NE MUST
ne MUST match every document where eq with the same operand does not match, including documents where the field is null or missing. nin MUST likewise match every document where in does not.
NULL_RANGE MUST
gt, gte, lt, lte and like MUST NOT match documents where the field is null or missing.
NO_COERCION MUST
Conditions MUST compare values without type conversion: a value matches only operands of the same type, and range operators MUST NOT match values of a different type than the operand.
A provider MUST fail on a condition that it cannot translate with the semantics of this specification, and MUST NOT approximate it. A provider MAY rely on an assumption about the stored data, such as the type of a field, only when it documents that assumption.
1.3.1 Equality and order
Section titled “1.3.1 Equality and order”EQUALITY_VALUES MUST
Equality MUST compare values by type and content: numbers by numeric value (1 equals 1.0), plain objects by their members without regard to order (a member whose value is undefined counts as missing, NULL_UNDEFINED), and arrays element by element, in order. Values of different types, such as a JSON number and a $bigint, MUST NOT be equal.
EQUALITY_NUMBERS MUST
JSON numbers and ECSON $number values MUST be compared as one type, number. Negative zero MUST be equal to zero, and NaN MUST be equal to NaN.
EQUALITY_ZONED MUST
Two $zoneddatetime values MUST be equal only when their instant, time zone and calendar are equal.
EQUALITY_DECIMAL MUST
Values of the BSON $numberDecimal type MUST be compared by numeric value, for equality and for order (1.0 equals 1.00), and only with other $numberDecimal values.
ORDER_TYPES MUST
The operand of a range operator (gt, gte, lt, lte) MUST be of an orderable type: a number (a JSON number or an ECSON $number), a string, a $bigint, a $numberDecimal, a $date, or a Temporal $instant, $zoneddatetime, $plaindate, $plaindatetime, $plaintime, $plainyearmonth or $duration. Any other operand, including null, booleans, arrays and objects, MUST make the filter fail. -Infinity and Infinity are the lowest and the highest numbers.
ORDER_TEMPORAL MUST
$zoneddatetime values MUST be ordered by their instant, whatever their time zone and calendar. $duration values MUST be ordered by their length, with days of 24 hours.
ORDER_UNORDERABLE MUST
A $duration with years, months or weeks, a NaN number or decimal and an invalid $date are not orderable: as the operand of a range operator they MUST make the filter fail (ORDER_TYPES), and as a field value they MUST NOT match a range operator.
An implementation that cannot compare values of a type as section 1.3.1 requires MUST make the filter fail on a condition whose operand has that type, such as an implementation that reads $numberDecimal as an unknown tag (ECSON section 7) and cannot compare it by numeric value, or one that keeps Temporal values as opaque strings (ECSON TYPE_TEMPORAL_OPAQUE) and cannot order them.
ORDER_STRINGS MUST
Range operators MUST order strings by Unicode code point, whatever the collation of the data source.
1.4 Core operators
Section titled “1.4 Core operators”OPS_CORE MUST
Every implementation MUST support the core operators: eq, ne, gt, gte, lt, lte, in, nin, exists and like, and the logical combinations and, or, not and nor.
| Operator | JSON syntax (section 2) | Matches when the field value |
|---|---|---|
eq |
@eq |
equals the operand |
ne |
@ne |
does not equal the operand |
gt, gte, lt, lte |
@gt, @gte, @lt, @lte |
is greater than, greater than or equal to, less than, less than or equal to the operand |
in |
@in |
equals one of the values in the operand list; in [a, b] is equivalent to eq a or eq b, so a null in the list also matches missing fields (NULL_EQ) |
nin |
@nin |
equals none of the values in the operand list; the exact complement of in |
exists |
@exists |
exists (operand true) or does not exist (operand false) |
like |
@like |
is a string matching the operand pattern, where % matches any sequence of characters and _ matches exactly one character, case-sensitive |
LIKE_ESCAPE MUST
In a like pattern, \ followed by %, _ or \ MUST match that character literally. A \ followed by any other character, or at the end of the pattern, MUST make the filter fail.
1.5 Optional operator sets
Section titled “1.5 Optional operator sets”OPS_SETS MAY
An implementation MAY support any optional operator set, in whole. Partial support of a set is not compliant with that set.
| Set | Operators |
|---|---|
| Array | @all (the field is an array that contains every value in the operand list), @size (the field is an array with exactly the operand number of elements; each nested array counts as one element), @elemMatch (at least one element of the array field matches the operand, section 1.5.2) |
| Regex | @regex (the string field matches a regular expression, given as a string or as an ECSON $regexp value, section 1.5.3) |
| Geospatial | @geoWithin, @geoIntersects, @near, with GeoJSON operands (section 1.5.1) |
OPS_UNSUPPORTED_FAIL MUST
An implementation MUST fail when a filter uses an operator it does not support, and MUST NOT ignore the condition.
1.5.1 Geospatial set
Section titled “1.5.1 Geospatial set”GEO_GEOJSON MUST
Operands of the geospatial set MUST be GeoJSON geometry objects (RFC 7946), with positions in WGS 84 as longitude, then latitude. Distances MUST be in meters.
GEO_GEOJSON_MEMBERS MUST
Implementations MUST accept the bbox member and foreign members of GeoJSON objects and ignore them (RFC 7946, section 5 and section 6.1).
GEO_GEOJSON_EMPTY MUST
A geometry whose coordinates is an empty array, or a GeometryCollection without geometries, MUST make the filter fail. A line with fewer than two positions or a ring with fewer than four is invalid GeoJSON (GEO_GEOJSON).
GEO_FIELD MUST
A geospatial condition MUST match only documents whose field holds a GeoJSON geometry object. Missing fields, null and other values MUST NOT match.
| Operator | Operand | Matches when |
|---|---|---|
@geoWithin |
a Polygon or MultiPolygon |
the geometry of the field lies entirely inside the operand |
@geoIntersects |
any geometry | the geometry of the field and the operand have at least one point in common |
@near |
{ "point": <Point>, "maxDistance": <meters>, "minDistance": <meters> }, with maxDistance required and minDistance optional (default 0) |
the distance between the geometry of the field and point is at least minDistance and at most maxDistance |
GEO_OPERATORS MUST
The geospatial operators MUST match as specified in the table above.
GEO_NEAR_NO_SORT MUST NOT
@near MUST NOT change the order of the results: like every condition, it only decides whether a document matches.
GEO_DISTANCE MUST
Distances MUST be measured on the surface of the Earth, on a sphere or on the WGS 84 spheroid.
1.5.2 Array set
Section titled “1.5.2 Array set”@all, @size and @elemMatch MUST NOT match a field that is missing, null or not an array.
ELEM_MATCH MUST
A field with @elemMatch MUST match when it holds an array with at least one element that matches the whole operand. When every key of the operand starts with @, the operand is a set of conditions applied to the element itself; otherwise the operand is a filter applied with the element as the document. ARRAY_SET_ARRAYS_ONLY covers fields that are not arrays.
ELEM_MATCH_FORMS MUST
In the form with only @ keys, the operand is an operator object, as under a field path (JSON_OPERATOR_OBJECT): @not takes an operator object, and @and, @or and @nor MUST make the filter fail.
ELEM_MATCH_EMPTY MUST
The operand {} belongs to the form with only @ keys and MUST match every array with at least one element.
In the filter form, an element that is not a plain object MUST be treated as an object with no fields.
ELEM_MATCH_OPERAND MUST
An operand of @elemMatch that is not an object MUST make the filter fail.
1.5.3 Regex set
Section titled “1.5.3 Regex set”REGEX_DIALECT MUST
An implementation that evaluates filters itself MUST interpret @regex patterns as ECMAScript regular expressions. A provider MUST apply the dialect of the regular expressions of its data source.
REGEX_PROVIDER_PASSTHROUGH MUST
A provider MUST pass the pattern to the regular expression engine of its data source unchanged, as a bound value.
A provider MUST fail when its data source has no regular expressions, or cannot express a flag of the pattern that changes whether a string matches.
REGEX_FLAGS MUST
The flags d and g do not change whether a string matches and MUST be ignored: each string is matched from its start, as with lastIndex 0. The flag y MUST make the match start at the beginning of the string.
Source: lingodata/unquery@865de91 · spec/sections/01-query-model.md · unquery 0.1.0 (draft)
LingoData specifications, reference implementations and this site are licensed under the Apache License 2.0. Copyright 2026 Gustavo Gondim.