Other Functions
Type checking, type casting, conditional logic, object manipulation, and utility functions.
Type Checking Functions
Functions to check the type of values at runtime.
IS_DATETIME(val)
Returns true if value is an ISO 8601 date string.
RETURN IS_DATETIME("2024-01-15T10:30:00Z") --true
TYPENAME(val)
Returns the type name as a string.
RETURN TYPENAME([1,2]) --"array"
RETURN TYPENAME("hi") --"string"
IS_EMAIL(val)
Returns true if value is a valid email format.
RETURN IS_EMAIL("user@example.com") --true
IS_URL(val)
Returns true if value is a valid URL format.
RETURN IS_URL("https://example.com") --true
IS_UUID(val)
Returns true if value is a valid UUID format.
RETURN IS_UUID("550e8400-e29b-41d4-...") --true
Type Casting Functions
Functions to convert values between different types.
TO_BOOL(value)
Casts value to boolean.
RETURN TO_BOOL(null) --false
RETURN TO_BOOL(1) --true
RETURN TO_BOOL(0) --false
TO_NUMBER(value)
Casts value to number.
RETURN TO_NUMBER("123") --123
RETURN TO_NUMBER(true) --1
RETURN TO_NUMBER("foo") --0
Logical & Conditional
Functions for conditional logic and null handling.
IF(cond, true, false)
Condition evaluation. Returns true_val if cond is true, else false_val.
RETURN IF(1 > 0, "yes", "no") --"yes"
RETURN IF(null, 1, 0) --0
cond ? true : false
Ternary operator. Syntactic sugar for IF function.
RETURN doc.age >= 18 ? "Adult" : "Minor"
RETURN a ? b ? "both" : "a" : "none"
Object Functions
Functions for manipulating objects and documents.
DEEP_MERGE(obj1, obj2, ...)
Deep merge objects recursively.
RETURN DEEP_MERGE({a:{b:1}}, {a:{c:2}})
--{a:{b:1,c:2}}
REDACT(doc, keys)
Deep-remove keys from objects and nested objects/arrays.
RETURN REDACT(doc, ["ssn", "email"])
ATTRIBUTES(doc, removeInternal?, sort?)
Top-level attribute keys of the document.
RETURN ATTRIBUTES(doc, true)
Collection Functions
Usage Example
Get document counts for multiple collections:
RETURN {
users: COLLECTION_COUNT("users"),
orders: COLLECTION_COUNT("orders"),
products: COLLECTION_COUNT("products")
}
Auth, sketches, events, RAG
CURRENT_USER() / CURRENT_ROLES() / CAN(action [, doc])
Request principal. HTTP queries get the JWT user. Without a principal, CAN is false and CURRENT_USER is null. Actions: read, write, admin. Optional doc also checks owner/_owner and _acl.{action} (user, role, or *).
FILTER CAN("write")
RETURN CURRENT_USER()
ROW_POLICY(coll [, pred])
Store a filter expression on the collection. Non-admin FOR scans evaluate it with the doc bound as the collection name and as doc. Pass null to clear.
RETURN ROW_POLICY("orders", "orders.tenant == @tenant")
PARSE_IDENTIFIER / PARSE_COLLECTION / PARSE_KEY
Split a document id collection/key. Extra slashes stay in the key.
RETURN PARSE_IDENTIFIER("users/ada")
-- { collection: "users", key: "ada" }
UNSET_RECURSIVE / KEEP_RECURSIVE / ZIP_OBJECT
Deep drop or keep named keys. ZIP_OBJECT(keys, values) builds an object (pair-wise ZIP is unchanged for numeric arrays).
RETURN UNSET_RECURSIVE(doc, "ssn")
RETURN ZIP_OBJECT(["a","b"], [1,2])
APPLY(name, args) / CALL(name, …)
Call another builtin by name. Recursion is capped at 8.
RETURN CALL("UPPER", "hi")
RETURN APPLY("ABS", [-3])
MINHASH / MINHASH_COUNT / MINHASH_ERROR
MinHash signatures for Jaccard-style set comparison. MINHASH_COUNT(0.05) is 400 hashes.
RETURN MINHASH(["a","b","c"], 16)
APPROX_COUNT_DISTINCT / APPROX_PERCENTILE / APPROX_TOP_K / SKETCH_MERGE
HyperLogLog (p=14), percentile, Space-Saving top-k. HLL results are objects with _type: "hll" and estimate; merge two sketches with SKETCH_MERGE.
RETURN APPROX_PERCENTILE(amounts, 95)
MATCH_SEQ(events, key, steps)
Find ordered event sequences per key. Steps: {as, type, field, equals, within}. Times use ts / t / time.
RETURN MATCH_SEQ(events, "user", [
{as: "a", type: "signup"},
{as: "b", type: "pay", within: "7d"}
])
SNAPSHOT_DIFF(coll, t1, t2)
On a versioned collection, classify documents inserted, updated, or deleted between two timestamps.
RETURN SNAPSHOT_DIFF("orders", t1, t2)
EMBED / EXTRACT / CITE / GROUNDED / SEMANTIC
EMBED and EXTRACT call the configured LLM (same providers as auto-embed). CITE / GROUNDED fall back to lexical overlap. SEMANTIC(doc, q) is a trigram score.
RETURN CITE(@answer, results)
RETURN EMBED(doc.body, { provider: "openai" })
Hashing & Encoding
Functions for hashing strings and encoding/decoding data.
Unique ID Generation
Functions for generating unique identifiers for documents and keys.
ID Type Comparison
| Type | Length | Sortable | Best For |
|---|---|---|---|
| UUID v4 | 36 chars | No | General unique identifiers |
| UUID v7 | 36 chars | Yes | Time-ordered records, primary keys |
| ULID | 26 chars | Yes | Compact time-sortable IDs |
| Nano ID | 21 chars | No | URL-safe short IDs, user-facing |