Filtering with the Entity Service API
The Entity Service API is deprecated in Strapi v5. Please consider using the Document Service API instead.
The Entity Service API offers the ability to filter results found with its findMany() method.
Results are filtered with the filters parameter that accepts logical operators and attribute operators. Every operator should be prefixed with $.
For examples of how to deep filter with the various APIs, please refer to this blog article.
Logical operators
$and
All nested conditions must be true.
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
$and: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
$and will be used implicitly when passing an object with nested conditions:
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: 'Hello World',
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
});
$or
One or many nested conditions must be true.
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
$or: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
$not
Negates the nested conditions.
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
$not: {
title: 'Hello World',
},
},
});
$not can be used as:
- a logical operator (e.g. in
filters: { $not: { // conditions… }}) - an attribute operator (e.g. in
filters: { attribute-name: $not: { … } }).
$and, $or and $not operators are nestable inside of another $and, $or or $not operator.
Attribute Operators
Using these operators may give different results depending on the database's implementation, as the comparison is handled by the database and not by Strapi.
$not
Negates the nested condition(s).
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$not: {
$contains: 'Hello World',
},
},
},
});
$eq
Attribute equals input value.
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$eq: 'Hello World',
},
},
});
$eq can be omitted:
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: 'Hello World',
},
});
$eqi
Attribute equals input value (case-insensitive).
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$eqi: 'HELLO World',
},
},
});
$ne
Attribute does not equal input value.
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$ne: 'ABCD',
},
},
});
$nei
Attribute does not equal input value (case-insensitive).
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$nei: 'abcd',
},
},
});
$in
Attribute is contained in the input list.
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$in: ['Hello', 'Hola', 'Bonjour'],
},
},
});
$in can be omitted when passing an array of values:
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: ['Hello', 'Hola', 'Bonjour'],
},
});
$notIn
Attribute is not contained in the input list.
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
title: {
$notIn: ['Hello', 'Hola', 'Bonjour'],
},
},
});
$lt
Attribute is less than the input value.
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
rating: {
$lt: 10,
},
},
});
$lte
Attribute is less than or equal to the input value.
Example
const entries = await strapi.entityService.findMany('api::article.article', {
filters: {
rating: {
$lte: 10,
},
},
});