Documentation Index
Fetch the complete documentation index at: https://mintlify.com/abejarano/ts-mongo-criteria/llms.txt
Use this file to discover all available pages before exploring further.
MongoCriteriaConverter bridges the library’s domain-level Criteria object and the raw query shape that the MongoDB Node.js driver expects. Call converter.convert(criteria) and you get back a MongoQuery you can feed directly into find, sort, skip, and limit calls — or let MongoRepository.list() do it for you automatically.
MongoQuery interface
convert() always returns an object that satisfies MongoQuery:
The MongoDB filter document derived from the
Criteria’s Filters. Each Filter in the criteria becomes a top-level key (or an $or array). When the criteria has no filters this is an empty object {}.The sort specification. A value of
1 means ascending, -1 means descending. When no order is specified the default is { _id: -1 } (newest-first by insertion order). When the orderBy field is the string "id" it is automatically remapped to "_id" for MongoDB compatibility.The number of documents to skip, derived from the page offset. When no offset is provided (or the criteria has no limit) this is
0. For a page-based criteria constructed as new Criteria(filters, order, limit, page), skip is computed as (page - 1) * limit.The maximum number of documents to return. Maps directly to the
limit value on the Criteria. 0 means no limit (the MongoDB driver returns all matching documents).convert(criteria: Criteria): MongoQuery
The primary public method. Converts a fully-constructed Criteria into a MongoQuery.
| Name | Type | Description |
|---|---|---|
criteria | Criteria | The criteria object encapsulating filters, order, limit, and page. |
MongoQuery
Protected methods
These methods areprotected so you can override them in a subclass for custom translation logic.
generateFilter(filters: Filters): MongoFilter
Iterates filters.filters and maps each Filter to a MongoDB filter fragment using the operator transformer registry. The fragments are merged with Object.assign so multiple filters on different fields are combined into a single flat object.
Error if an Operator value has no registered transformer.
generateSort(order: Order): MongoSort
Converts an Order value object into a MongoDB sort document. The orderBy field "id" is remapped to "_id".
Default behaviors
| Scenario | Resulting value |
|---|---|
Criteria has no filters (Filters.none()) | filter: {} |
Criteria has no order (Order.none()) | sort: { _id: -1 } |
Criteria has no offset / page | skip: 0 |
Criteria has no limit | limit: 0 (no limit) |
Operator mapping
EveryOperator enum value maps to a MongoDB query operator:
Operator | MongoDB operator | Notes |
|---|---|---|
EQUAL (=) | $eq | Strict equality |
NOT_EQUAL (!=) | $ne | Inequality |
GT (>) | $gt | Greater than |
LT (<) | $lt | Less than |
GTE (>=) | $gte | Greater than or equal |
LTE (<=) | $lte | Less than or equal |
CONTAINS | $regex | Substring / pattern match |
NOT_CONTAINS | $not: { $regex } | Negated pattern match |
BETWEEN | $gte + $lte | Requires { start, end }, { startDate, endDate }, or { from, to } value |
IN | $in | Requires an array of primitive values |
NOT_IN | $nin | Requires an array of primitive values |
OR | $or | Requires an array of OrCondition objects |
Example
BETWEEN date range
TheBETWEEN operator accepts any of three value shapes: { start, end }, { startDate, endDate }, or { from, to }. All three are normalised to { start, end } internally before producing the $gte/$lte filter.
OR across multiple fields
Subclassing
You can extendMongoCriteriaConverter to alter how filters or sorts are generated — for example to support case-insensitive regex or a custom sort field alias:
MongoRepository by overriding criteriaConverter if needed, or use it standalone for query building.