Documentation / @andrew_l/mongo-pagination / mergeFilters
Function: mergeFilters()
mergeFilters(
first
,second
):Record
<string
,any
>
Merges two MongoDB filters in the most effective way.
This function combines two filters, prioritizing the most specific criteria from both and ensuring the merged filter works efficiently for MongoDB queries.
Parameters
• first: Record
<string
, any
>
The first MongoDB filter object.
• second: Record
<string
, any
>
The second MongoDB filter object.
Returns
Record
<string
, any
>
The merged MongoDB filter object, combining both filters.
Example
ts
// Basic usage:
const filter1 = { status: 'active', age: { $gt: 18 } };
const filter2 = { age: { $lt: 60 }, country: 'USA' };
const mergedFilter = mergeFilters(filter1, filter2);
console.log(mergedFilter);
// Output: { status: 'active', age: { $gt: 18, $lt: 60 }, country: 'USA' }