The DataFilter is a powerful utility used to refine queries in MongoDB by applying specific conditions to the data. It can be created by calling the client.filter() method, which initializes a new filter object that you can chain with various methods to build complex queries.
To create a DataFilter, you simply call the filter() method on your client instance (learn how to create one):
const filter = client.filter();Once created, the filter object can be used to apply various conditions to your queries.
Here’s a breakdown of the key methods available in DataFilter:
eq(property: string, value: string | number | boolean | Date, options?: { caseSensitive: boolean }): DataFilterThis method matches items whose specified property value does not equal the specified value.
Configure with options:
{
/** Whether the comparison should be case-sensitive (only applies to string values). Default: `true` */
caseSensitive: boolean;
}Example:
filter.eq('loginEmail', 'email@domain.com');ne(property: string, value: string | number | boolean | Date, options?: { caseSensitive: boolean }): DataFilterThis method matches items whose specified property value does not equal the specified value.
Configure with options:
{
/** Whether the comparison should be case-sensitive (only applies to string values). Default: `true` */
caseSensitive: boolean;
}Example:
filter.ne('username', 'admin', { caseSensitive: false });contains(property: string, value: string, options?: { caseSensitive: boolean }): DataFilterThis method matches items whose specified property value contains the specified string.
Configure with options:
{
/** Whether the comparison should be case-sensitive. Default: `false` */
caseSensitive: boolean;
}Example:
filter.contains('bio', 'developer');startsWith(property: string, value: string, options?: { caseSensitive: boolean }): DataFilterMatches items whose specified property value starts with the given substring.
Configure with options:
{
/** Whether the comparison should be case-sensitive. Default: `false` */
caseSensitive: boolean;
}Example:
filter.startsWith('slug', 'user-');endsWith(property: string, value: string, options?: { caseSensitive: boolean }): DataFilterMatches items whose specified property value ends with the given substring.
Configure with options:
{
/** Whether the comparison should be case-sensitive. Default: `false` */
caseSensitive: boolean;
}Example:
filter.endsWith('email', '@example.com');gt(property: string, value: string | number | Date): DataFilterMatches items where the property is greater than the specified value.
Example:
filter.gt('views', 1000);gte(property: string, value: string | number | Date): DataFilterMatches items where the property is greater than or equal to the specified value.
Example:
filter.gte('price', 99.99);lt(property: string, value: string | number | Date): DataFilterMatches items where the property is less than the specified value.
Example:
filter.lt('age', 18);lte(property: string, value: string | number | Date): DataFilterMatches items where the property is less than or equal to the specified value.
Example:
filter.lte('discount', 50);between(property: string, start: string | number | Date, end: string | number | Date): DataFilterThis method allows you to match items whose specified property value is within a given range. Both the start and end values can be a string, number, or Date.
Example:
filter.between('age', 18, 30);in(property: string, value: (string | number)[]): DataFilterMatches items where the property value is in the specified list.
Example:
filter.in('status', ['active', 'pending']);nin(property: string, value: (string | number)[]): DataFilterMatches items where the property value is not in the list or the property does not exist.
Example:
filter.nin('role', ['admin', 'moderator']);hasAll(property: string, value: (string | number | Date | any)[]): DataFilterMatches items where the array property contains all of the given values.
Example:
filter.hasAll('tags', ['technology', 'javascript']);hasSome(property: string, value: (string | number | Date | any)[]): DataFilterMatches items where the array property contains at least one of the given values.
Example:
filter.hasSome("roles", ["admin", "moderator"]);and(filter: DataFilter | DataFilter[]): DataFilterMatches items where all conditions are true.
Example:
const activeFilter = client.filter().eq('status', 'active');
const goodScoreFilter = client.filter().gte('score', 80);
// Add the two filters above to the main one below
const filter = client.filter().and([activeFilter, goodScoreFilter]);or(filter: DataFilter | DataFilter[]): DataFilterMatches items where at least one condition is true.
Example:
const filter = client.filter().or([
client.filter().eq('tier', 'gold')
client.filter().eq('tier', 'platinum')
])nor(filter: DataFilter | DataFilter[]): DataFilterMatches items where none of the conditions are true.
Example:
filter.nor([
client.filter().eq('role', 'banned'),
client.filter().eq('role', 'suspended')
]);not(filter: DataFilter): DataFilterNegates the result of the provided filter.
Example:
filter.not(client.filter().lt('credits', 1));exists(property: string, value: boolean): DataFilterMatches items where the property exists or does not exist, including when the value is null.
Example:
filter.exists('profilePicture', true);type(property: string, value: DataBSONType | DataBSONType[]): DataFilterMatches items where the property value is of a specified BSON type.
Example:
filter.type('createdAt', 'date');