Skip to content

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 ​

TypeOperators
stringcontains, equals, notEquals, startsWith, endsWith, oneOf (comma list), isEmpty, isNotEmpty
numberequals, notEquals, gte, lte, between, oneOf, isEmpty, isNotEmpty
date-timegte, lte, between, isEmpty, isNotEmpty
booleanisTrue, 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, objectisEmpty, 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 ​

FunctionDescription
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: true excludes what the combined conditions match. It is written as -( … ) (see useMongocampQuery). 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 oneOf list 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 name works); range bounds are additionally accepted here only as plain numbers or ISO dates.
  • contains keeps * and ? as wildcards, so a*b matches "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=true with a wildcard filter unless Match case is on (see useMongocampQuery).
  • Numbers keep the digits as typed and are written unquoted (age:30); only plain decimals are accepted (no 1e3). 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-01 is [2024-01-01 TO *], lte 2024-01-01 is [* 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 in useMongocampQuery().inRange; this composable only validates what was typed.
  • _id takes equals / notEquals / oneOf with 24-character hex ids, and startsWith / endsWith / contains with 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.

Released under the MIT License.