Skip to content

@xtandard/lib/filters/mongo

Renders the compiled filter AST to a Mongo filter object (no driver dependency). Works with the MongoDB driver and Mongoose alike, since it returns plain objects.

Text matching becomes $regex; between → $gte/$lte; inArray → $in. like/ilike translate the SQL LIKE pattern to a regex (%→.*, _→.). The PG array ops map natively: arrayContains (@>) → $all, arrayOverlaps (&&) → $in, and arrayContained (<@, field ⊆ values) → an $expr with $setIsSubset (Mongo has no single-symbol “contained by” operator).

import { buildFilter, textField, numberField } from "@xtandard/lib/filters/mongo";
const spec = { name: textField({ path: "name" }), amount: numberField({ path: "amount" }) };
const { filter } = buildFilter({ spec, filters, resolveDate });
// filter → { $and: [{ name: { $regex: "ab", $options: "i" } }, … ] }
const docs = await collection.find(filter ?? {}).toArray();
import { … } from "@xtandard/lib/filters/mongo";
Name Summary
buildFilter Compile a flat, AND-combined list of column filters into a Mongo filter object. Only fields in spec whose kind matches the filter’s kind are kept; the rest are silently dropped. filter is undefined when nothing survives, so pass filter ?? {} to find. A date preset filter needs resolveDate (it throws without one).
buildFilterNode Compile a recursive and/or/not FilterNode tree into a Mongo filter object (or undefined). Same allow-list rules as buildFilter; not becomes $nor: [inner].
toMongoFilter Render an already-compiled CompiledWhere AST (from compileFilters / compileFilterNode in @xtandard/lib/filters) to a Mongo filter object, looking each field up in paths. Conditions on fields missing from paths are dropped; null (or an AST that drops to nothing) gives undefined. and/or/not become $and/$or/$nor. Most code calls buildFilter, which compiles and renders in one step.
Name Summary
MongoFieldSpec One allow-listed field: its filter kind plus the document field path ("name", "address.city") it reads.
MongoFilter A plain Mongo query filter object, ready for collection.find(filter) or Mongoose.
MongoFilterSpec A resource’s filter allow-list: public field name → MongoFieldSpec.
Name Summary
arrayField Allow-list a public field as an array filter over a document field path holding an array ("tags"). arrayContains → $all, arrayOverlaps → $in, and arrayContained → a top-level $expr with $setIsSubset.
booleanField Allow-list a public field as a boolean filter (eq/ne/null checks) over a document field path ("done").
dateField Allow-list a public field as a date filter over a document field path ("createdAt", dot paths allowed) holding a Date. A date preset needs the resolveDate you pass to buildFilter, and becomes a half-open $gte/$lt window.
enumField Allow-list a public field as an enum filter (eq/ne/inArray/notInArray/null checks) over a document field path ("status").
numberField Allow-list a public field as a number filter over a document field path ("total", "stats.views"). between becomes $gte + $lte.
textField Allow-list a public field as a text filter over a document field path ("name", "address.city"). contains/startsWith/endsWith become a case-insensitive $regex (input escaped); like/ilike patterns are translated to a regex.