Skip to content

Documentation / @andrew_l/mongo-pagination / createRangeFilter

Function: createRangeFilter()

createRangeFilter(sortDirection, sortValues): Record<string, any>

Creates a range filter based on sorting direction and values.

This function generates a range filter to be used in queries, typically for pagination, based on the provided sorting direction (ascending or descending) and the sorting values (field names and values).

Parameters

sortDirection: Sort

The sorting direction, which could be ascending or descending.

sortValues: Record<string, any>

The sorting values, including field names and their respective values for sorting.

Returns

Record<string, any>

A range filter object that can be used in MongoDB.

Example

ts
// Example usage
const sortDirection = { age: 1 };
const sortValues = { age: 30 };
const rangeFilter = createRangeFilter(sortDirection, sortValues);
console.log(rangeFilter);
// Output: { age: { $gt: 30 } }