Skip to main content

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.

Criteria is the central query object in ts-mongo-criteria. It bundles a Filters collection, an Order specification, and optional pagination parameters into a single value that MongoRepository.list() (or your own repository) consumes to build and execute a MongoDB query.

Constructor

new Criteria(filters: Filters, order: Order, limit?: number, offset?: number)
filters
Filters
required
The set of filter conditions to apply to the query. Pass Filters.none() to retrieve all documents without filtering.
order
Order
required
The sort specification for the query. Pass Order.none() to use the repository’s default sort (typically { _id: -1 }).
limit
number
Maximum number of documents to return per page. When omitted the repository returns all matching documents.
offset
number
1-based page number, not a raw skip value. The repository converts this to a MongoDB skip of (offset - 1) * limit. When omitted the query starts from the first document.

Properties

PropertyTypeDescription
filtersFiltersThe Filters instance passed to the constructor.
orderOrderThe Order instance passed to the constructor.
limitnumber | undefinedMaximum documents per page.
currentPagenumber | undefinedThe 1-based page number as supplied to the constructor. This is the raw value you pass in.
offsetnumber | undefinedThe computed MongoDB skip value: (currentPage - 1) * limit. undefined when limit or the page number is not provided.

Methods

hasFilters(): boolean

Returns true when the Filters collection contains at least one Filter condition, false otherwise. Useful for conditionally logging or short-circuiting query logic.
const criteria = new Criteria(Filters.none(), Order.none());
criteria.hasFilters(); // false

const activeCriteria = new Criteria(
  Filters.fromValues([new Map([["field", "status"], ["operator", "="], ["value", "active"]])]),
  Order.none()
);
activeCriteria.hasFilters(); // true

Usage Example

import {
  Criteria,
  Filters,
  Filter,
  FilterField,
  FilterOperator,
  FilterValue,
  Operator,
  Order,
} from "@abejarano/ts-mongodb-criteria";

// Build individual filter conditions
const statusFilter = new Filter(
  new FilterField("status"),
  new FilterOperator(Operator.EQUAL),
  new FilterValue("active")
);

const scoreFilter = new Filter(
  new FilterField("score"),
  new FilterOperator(Operator.GTE),
  new FilterValue(80)
);

// Combine into a Filters collection
const filters = new Filters([statusFilter, scoreFilter]);

// Sort by createdAt descending
const order = Order.desc("createdAt");

// Page 2, 25 documents per page
//   → MongoDB: skip = (2 - 1) * 25 = 25, limit = 25
const criteria = new Criteria(filters, order, 25, 2);

console.log(criteria.hasFilters()); // true
console.log(criteria.limit);        // 25
console.log(criteria.currentPage);  // 2  (page number supplied to constructor)
console.log(criteria.offset);       // 25 (computed MongoDB skip: (2 - 1) * 25)

// Pass to your repository
const results = await userRepository.list(criteria);
The constructor’s offset parameter is a 1-based page number. Internally, Criteria stores the page number in currentPage and pre-computes the MongoDB skip into offset as (currentPage - 1) * limit. If you supply page 3 with limit: 10, criteria.offset will be 20 — the raw skip value that MongoDB receives. Always pass 1-based page numbers to the constructor.

Build docs developers (and LLMs) love