Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Filter Operators

Filters are used in crap.collections.find() queries and in access control constraint returns. They map to SQL WHERE clauses.

Shorthand: Simple Equality

String values are treated as equals:

{ where = { status = "published" } }
-- SQL: WHERE status = 'published'

Operator Syntax

Use a table to specify an operator:

{ where = { title = { contains = "hello" } } }
-- SQL: WHERE title LIKE '%hello%'

Operator Reference

OperatorLua SyntaxSQLDescription
equals{ equals = "value" }field = ?Exact match
not_equals{ not_equals = "value" }field != ?Not equal
like{ like = "pattern%" }field LIKE ?SQL LIKE pattern
contains{ contains = "text" }field LIKE '%text%' ESCAPE '\'Substring match (wildcards % and _ in the search text are escaped)
greater_than{ greater_than = "10" }field > ?Greater than
less_than{ less_than = "10" }field < ?Less than
greater_than_or_equal{ greater_than_or_equal = "10" }field >= ?Greater than or equal
less_than_or_equal{ less_than_or_equal = "10" }field <= ?Less than or equal
in{ ["in"] = { "a", "b" } }field IN (?, ?)Value in list
not_in{ not_in = { "a", "b" } }field NOT IN (?, ?)Value not in list
exists{ exists = true }field IS NOT NULLField is not null (value is ignored — only the key matters)
not_exists{ not_exists = true }field IS NULLField is null (value is ignored — use not_exists for IS NULL, not { exists = false })

Note: in is a Lua keyword, so use ["in"] bracket syntax.

Examples

-- Published posts containing "hello"
crap.collections.posts.find({
    where = {
        status = "published",
        title = { contains = "hello" },
    },
})

-- Posts created after a date
crap.collections.posts.find({
    where = {
        created_at = { greater_than = "2024-01-01" },
    },
})

-- Posts with specific statuses
crap.collections.posts.find({
    where = {
        status = { ["in"] = { "draft", "published" } },
    },
})

-- Posts without a category
crap.collections.posts.find({
    where = {
        category = { not_exists = true },
    },
})

-- Posts with wildcard title match
crap.collections.posts.find({
    where = {
        title = { like = "Hello%" },
    },
})

Multiple Filters

Multiple filters are combined with AND:

crap.collections.posts.find({
    where = {
        status = "published",
        created_at = { greater_than = "2024-01-01" },
        title = { contains = "update" },
    },
})
-- SQL: WHERE status = ? AND created_at > ? AND title LIKE ?

OR Groups

Use the ["or"] key to combine groups of conditions with OR logic. Each element is a table of AND-ed conditions:

-- title contains "hello" OR category = "news"
crap.collections.posts.find({
    where = {
        ["or"] = {
            { title = { contains = "hello" } },
            { category = "news" },
        },
    },
})
-- SQL: WHERE (title LIKE '%hello%' OR category = ?)

OR can combine with top-level AND filters:

-- status = "published" AND (title contains "hello" OR title contains "world")
crap.collections.posts.find({
    where = {
        status = "published",
        ["or"] = {
            { title = { contains = "hello" } },
            { title = { contains = "world" } },
        },
    },
})
-- SQL: WHERE status = ? AND (title LIKE '%hello%' OR title LIKE '%world%')

Each OR element can have multiple fields (AND-ed within the group):

-- (status = "published" AND title contains "hello") OR (status = "draft")
crap.collections.posts.find({
    where = {
        ["or"] = {
            { status = "published", title = { contains = "hello" } },
            { status = "draft" },
        },
    },
})
-- SQL: WHERE ((status = ? AND title LIKE '%hello%') OR status = ?)

Note: or is not a Lua keyword, but ["or"] bracket syntax is recommended for clarity.

Nested Field Filters (Dot Notation)

Filter on sub-fields of group, array, blocks, and has-many relationship fields using dot notation:

-- Group sub-field: seo.meta_title → seo__meta_title column
crap.collections.pages.find({
    where = { ["seo.meta_title"] = { contains = "SEO" } },
})

-- Array sub-field: find products with any variant color "red"
crap.collections.products.find({
    where = { ["variants.color"] = "red" },
})

-- Block sub-field: find posts with any content block containing "hello"
crap.collections.posts.find({
    where = { ["content.body"] = { contains = "hello" } },
})

-- Block type filter
crap.collections.posts.find({
    where = { ["content._block_type"] = "image" },
})

-- Has-many relationship: find posts with a specific tag
crap.collections.posts.find({
    where = { ["tags.id"] = "tag-123" },
})

Array and block filters use EXISTS subqueries — they match parent documents where at least one row matches. All filter operators work with dot notation paths.

See Query & Filters for the full reference.

Value Types

Filter values are coerced to a SQL bind type based on the field’s declared type, so comparisons run on the right kind of column (REAL for numbers, INTEGER for booleans, TEXT for ISO date strings, etc.). Coercion only applies to comparison operators — text-pattern operators (like, contains, starts_with, ends_with, regex) always bind as TEXT regardless of the field type.

Field typeComparison bind typeNotes
numberREALFilter value is parsed as a 64-bit float. Invalid input (non-numeric, NaN, Infinity) falls back to TEXT and logs a warning — the query still runs and will typically return no rows.
checkboxINTEGER (0 / 1)Accepts "true", "false", "1", "0", "yes", "no", "on", "off". Anything else falls back to TEXT with a warning.
dateTEXT (normalized ISO)Stored as ISO-8601 strings; the filter value is normalized via the date coercer (e.g. "2024-01-15" → "2024-01-15T00:00:00.000Z") so lexicographic comparison aligns with stored values.
text, textarea, email, select, radio, json, code, richtextTEXTBound as-is.
Field type unknown / not resolvedTEXTDefault fallback when the caller cannot determine the field type.
-- Number field — bound as REAL, comparison is numeric
crap.collections.products.find({
    where = { price = { greater_than = 9.99 } },
})

-- Checkbox field — bound as INTEGER 1
crap.collections.posts.find({
    where = { published = true },
})

-- Text-only operator on a number field still binds as TEXT
crap.collections.products.find({
    where = { sku = { contains = "42" } },
})