useMongocampQuery
High-level Lucene query helpers for MongoCamp collection filters. Every helper returns a Lucene expression string; a return value of '' means "no filter" and is safe to pass straight into and/or — they filter empty parts out automatically.
const h = useMongocampQuery()
h.and(
h.or(h.like('name', 'alice'), h.like('email', 'alice')),
h.inClause('role', ['admin', 'user']),
h.between('createdAt', '2024-01-01', null),
)
// => '((name:*alice* OR email:*alice*) AND role:("admin" OR "user") AND createdAt:[2024-01-01 TO *])'Value type
type QueryValue = string | number | boolean | null | undefinednull/undefined (and, for most helpers, empty strings) produce an empty/no-op expression rather than throwing — convenient for building a filter from a form where fields may be unset.
Helpers
| Helper | Produces | Notes |
|---|---|---|
exists(field) | _exists_:field | the field is present, whatever its type — see Existence |
notExists(field) | -_exists_:field | the field is absent |
isEmpty(field) | same as notExists | alias |
isNotEmpty(field) | same as exists | alias |
equals(field, value) | field:"value" | '' if value is nullish/empty |
notEquals(field, value) | -field:"value" | |
inRange(field, from, to) | field:[from TO to] | inclusive; null / '' / '*' is an open end — see Ranges |
notInRange(field, from, to) | -field:[from TO to] | open ends as in inRange |
between(field, from, to) | same as inRange | alias |
like(field, value) | field:*value* | falls back to field:* when empty |
notLike(field, value) | -field:*value* | |
startsWith(field, value) | field:value* | |
notStartsWith(field, value) | -field:value* | |
endsWith(field, value) | field:*value | |
notEndsWith(field, value) | -field:*value | |
inClause(field, values) | field:("v1" OR "v2" ...) | numbers unquoted; '' for an empty array |
notInClause(field, values) | -field:"v1" AND -field:"v2" ... | '' for an empty array |
containsAll(field, values) | (field:"v1" AND field:"v2" ...) | '' for an empty array |
and(...parts) | parts joined with AND, wrapped in () | filters out empty parts; single survivor returned unwrapped |
or(...parts) | parts joined with OR, wrapped in () | same filtering behavior |
byId(id) | _id:"<hex>" | one document by id — the hex text or { $oid } as a read returns it; a 24-character hex ObjectId (any case), otherwise it throws a TypeError — '' would mean "no filter", every document |
byIds(ids) | _id:("a" OR "b" ...) | any of these documents; every id validated like byId; '' for an empty array |
isTrue(field) / isFalse(field) | field:true / field:false | a boolean field; a document without the field matches neither |
arrayContains(field, value) | field:"x" / field:5 | the array has this element (text quoted, numbers not); '' for an empty value |
hasItems(field) | field.0:* | an array with at least one element — see Arrays |
onDay(field, 'YYYY-MM-DD') | field:[YYYY-MM-DD TO YYYY-MM-DD] | the whole UTC day; throws for anything that is not a real calendar date |
notOnDay(field, 'YYYY-MM-DD') | -field:[the whole day] | everything except that day, documents without the field included |
not(part) | -(part) | a single clause becomes -clause (and back) — see Negating groups |
notAnd(...parts) | -(a AND b) | "not all of these"; same as not(and(...)) |
notOr(...parts) | -(a OR b) | "none of these"; same as not(or(...)) |
The optional type argument of exists, notExists, isEmpty, isNotEmpty and hasItems ('text', 'number', 'date') is still accepted so older calls compile, but it is ignored and deprecated (ExistsFieldType).
Case
Wildcards (like, startsWith, endsWith, …) match case-sensitively on the server. A request that should ignore case sends filterCaseInsensitive=true next to the filter; the module's components do that for every filter with an unescaped * or ? unless the user turned on Match case (see MongocampQueryBar). An exact filter (equals, ranges, …) never needs the flag.
Existence
exists is _exists_:field — MongoDB's $exists. It works for every type (text, numbers, dates, booleans, arrays, objects, nested paths), and like $exists a field holding null, '' or [] exists too.
h.exists('dateDeleted') // _exists_:dateDeleted
h.notExists('dateDeleted') // -_exists_:dateDeletedArrays
arrayContainsmatches when any element matches:tags:"a"finds["a","b"],nums:7finds[5, 7](a number element is written unquoted), a value with a space is escaped,containsAll('tags', ['x','y'])finds arrays holding both.hasItemsisfield.0:*— the first element exists, so it matches a non-empty array of any element type. An empty array and a missing field do not match.- Nested paths work as written:
equals('obj.k', 'v')→obj.k:"v"(the dots are left alone); a field name with a space is escaped (first\ name:"Ann").
Ids and days
_id:"<hex>" and _id:("a" OR "b") find documents by ObjectId; an id that does not exist simply matches nothing. Since server 2.2 a prefix or wildcard of hex characters also matches an ObjectId by its hex text, ignoring case (startsWith('_id', '6ad7') → _id:6ad7*). A bare date is midnight UTC for the server, but as the inclusive upper bound it covers its whole day (server 2.2), so onDay writes the day as [day TO day].
Negating groups
The server follows Lucene's rules, so -(A AND B) means "not both". not writes -( … ) around a group; a single term (a clause, a range, a field's value list) becomes -term, a term that is already negated loses its -, and an already parenthesised group is not wrapped twice.
h.notAnd('active:true', 'role:"admin"') // -(active:true AND role:"admin")
h.notOr('a:1', 'b:2') // -(a:1 OR b:2)
h.not('-a:1') // a:1
h.not(h.exists('f')) // -_exists_:fAn empty expression stays empty (no filter, never "match nothing"). Mixing AND and OR follows Lucene (a OR b AND c makes b and c required, a optional) — use and / or, which always add the parentheses.
Ranges
- Open ends are written as
*:inRange('age', 20, null)→age:[20 TO *]. Both ends open means "has a value" and returnsexists(field)(notInRangethen returnsnotExists). - Dates: a date without a time is midnight UTC. As the inclusive upper bound the server (2.2) covers that whole day, so "up to 2024-06-15" includes the 15th; the module writes every bound as given.
- Numbers: a numeric string keeps its digits, so ids beyond 2^53 aren't rounded.
- Safety: brackets, quotes, whitespace and other characters that could end the range or start a new clause are escaped in a bound, so user input can't add query syntax. Field names are escaped by every helper (
first nameworks; dots in MongoDB paths are left alone).
Known server limits
- A quoted value is exact (server 2.2):
*,?,:,(,)and an escaped backslash inside quotes are literal (name:"Ali*"finds only the textAli*). Write a wildcard unquoted, with an escaped space — aslike,startsWithandendsWithdo. - A quoted value that converts matches the converted value or the same text (server 2.2):
equals('note', '2024-06-15T12:00:00Z')finds a stored date and date-like text ({ $string });equals('zip', '12345')finds the number and the text. - Fuzzy (
~) and boost (^) terms are refused with400(seeuseMongocampQueryBuilder).
Building a filter from form state
const filter = ref({ name: '', status: '', createdFrom: null, createdTo: null })
const h = useMongocampQuery()
const luceneFilter = computed(() => h.and(
h.like('name', filter.value.name),
h.equals('status', filter.value.status),
h.between('createdAt', filter.value.createdFrom, filter.value.createdTo),
))Empty/unset fields drop out automatically — no manual if checks needed before calling documentApi.listDocumentsRaw({ filter: luceneFilter.value }).
For lower-level string building (escaping, ranges, chainable groups), see useMongocampQueryBuilder, which this composable is built on.