useMongocampFilterBuilder
Pure logic behind the visual filter builder: turns "field · operator · value" rows into one Lucene query string for the filter parameter of the document and file APIs. It has no UI and no Nuxt dependency — build the rows however you like and pass them in. (The builder component that edits these rows is a separate step.)
ts
const { buildFilterQuery } = useMongocampFilterBuilder()
const query = buildFilterQuery(
{
match: 'all', // 'all' = AND, 'any' = OR
negate: false, // optional: true = exclude what the conditions match (NOT)
conditions: [
{ id: '1', field: 'name', operator: 'contains', value: 'ali' },
{ id: '2', field: 'age', operator: 'between', value: '18', value2: '65' },
],
},
{ name: 'string', age: 'number' }, // field → type; unknown fields count as 'string'
)
// => '(name:*ali* AND age:[18 TO 65])'Operators per field type
| Type | Operators |
|---|---|
string | contains, equals, notEquals, startsWith, endsWith, oneOf (comma list), isEmpty, isNotEmpty |
number | equals, notEquals, gte, lte, between, oneOf, isEmpty, isNotEmpty |
date-time | gte, lte, between, isEmpty, isNotEmpty |
boolean | isTrue, isFalse, isEmpty, isNotEmpty |
objectId (the document _id) | equals, notEquals, oneOf — each value must be a 24-character hex id, otherwise the row is skipped |
array, object | isEmpty, isNotEmpty |
gte/lte/between are inclusive. isEmpty / isNotEmpty ("does not exist" / "exists") use useMongocampQuery's exists / notExists (_exists_:field), which work for every type — and, like MongoDB's $exists, count a field holding null, '' or [] as existing.
Functions
| Function | Description |
|---|---|
operatorsFor(type) | Operators valid for a type (string operators for an unknown type) |
valueCountFor(operator) | How many value inputs the operator needs: 0, 1, or 2 (between) |
buildCondition(condition, type) | Lucene for one row, or '' if the row is incomplete or invalid |
buildFilterQuery(group, fieldTypes) | All valid rows joined with AND/OR; '' means "no filter" |
countActiveConditions(group, fieldTypes) | How many rows contribute to the query (for a "Filters (n)" badge) |
Behaviour worth knowing
negate: trueexcludes what the combined conditions match. It is written as-( … )(seeuseMongocampQuery). A negated group with no valid row is "no filter", not "match nothing".- Incomplete or invalid rows are skipped, never turned into "match everything": empty values, non-numeric numbers, non-ISO dates, an operator that doesn't fit the field type. A
oneOflist with a single bad number is rejected as a whole. - Safe against query injection: values and field names are escaped by the query helpers (a field called
first nameworks); range bounds are additionally accepted here only as plain numbers or ISO dates. containskeeps*and?as wildcards, soa*bmatches "a…b". A leading wildcard (contains,endsWith) cannot use an index and is slow on large collections.- Wildcards match case-sensitively on the server; the query string itself carries no case rule. The components send
filterCaseInsensitive=truewith a wildcard filter unless Match case is on (seeuseMongocampQuery). - Numbers keep the digits as typed and are written unquoted (
age:30); only plain decimals are accepted (no1e3). Ids and account numbers beyond 2^53 therefore aren't rounded. Strings are written quoted (name:"ali"). - Dates: a bare date is midnight UTC —
gte 2024-01-01is[2024-01-01 TO *],lte 2024-01-01is[* TO 2024-01-01], which the server (2.2) reads as the whole day. A full timestamp is used as given. - Open-ended ranges (
gte,lte) are written with*for the open end. The date and range handling lives inuseMongocampQuery().inRange; this composable only validates what was typed. _idtakesequals/notEquals/oneOfwith 24-character hex ids, andstartsWith/endsWith/containswith 1–24 hex characters (server 2.2 matches them against the id's hex text, ignoring case:_id:6ad7e7*). Any other value skips the row. There is no existence test on_id.
See also useMongocampQuery.