Skip to main content

Document Service API: Filters

Page summary:

The Document Service API provides attribute operators ($eq, $lt, $contains, etc.) and logical operators ($and, $or, $not) to filter query results with support for case-sensitive and case-insensitive matching.

The Document Service API offers the ability to filter results.

The following operators are available:

OperatorDescription
$eqEqual
$eqiEqual (case-insensitive)
$neNot equal
$neiNot equal (case-insensitive)
$ltLess than
$lteLess than or equal to
$gtGreater than
$gteGreater than or equal to
$inIncluded in an array
$notInNot included in an array
$containsContains
$notContainsDoes not contain
$containsiContains (case-insensitive)
$notContainsiDoes not contain (case-insensitive)
$nullIs null
$notNullIs not null
$betweenIs between
$startsWithStarts with
$startsWithiStarts with (case-insensitive)
$endsWithEnds with
$endsWithiEnds with (case-insensitive)
$orJoins the filters in an "or" expression
$andJoins the filters in an "and" expression
$notJoins the filters in an "not" expression

Filter values are cast to the type of the filtered attribute. A value that cannot be cast throws a ValidationError (e.g., 'no' on a boolean attribute, or 'tomorrow' on a date attribute). For boolean attributes, the accepted values are listed in the REST API filters documentation.

Deep filtering with the various APIs

For examples of how to deep filter with the various APIs, please refer to this blog article.

Attribute operators​


strapi.documents().findMany()

$not

Negates the nested condition(s).

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$not: {
$contains: 'Hello World',
},
},
},
});
strapi.documents().findMany()

$eq

Attribute equals input value.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$eq: 'Hello World',
},
},
});
strapi.documents().findMany()

$eqi

Attribute equals input value (case-insensitive).

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$eqi: 'HELLO World',
},
},
});
strapi.documents().findMany()

$ne

Attribute does not equal input value.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$ne: 'ABCD',
},
},
});
strapi.documents().findMany()

$nei

Attribute does not equal input value (case-insensitive).

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$nei: 'abcd',
},
},
});
strapi.documents().findMany()

$in

Attribute is contained in the input list.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$in: ['Hello', 'Hola', 'Bonjour'],
},
},
});
strapi.documents().findMany()

$notIn

Attribute is not contained in the input list.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$notIn: ['Hello', 'Hola', 'Bonjour'],
},
},
});
strapi.documents().findMany()

$lt

Attribute is less than the input value.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
rating: {
$lt: 10,
},
},
});
strapi.documents().findMany()

$lte

Attribute is less than or equal to the input value.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
rating: {
$lte: 10,
},
},
});
strapi.documents().findMany()

$gt

Attribute is greater than the input value.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
rating: {
$gt: 5,
},
},
});
strapi.documents().findMany()

$gte

Attribute is greater than or equal to the input value.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
rating: {
$gte: 5,
},
},
});
strapi.documents().findMany()

$between

Attribute is between the 2 input values, boundaries included (e.g., $between[1, 3] will also return 1 and 3).

const entries = await strapi.documents('api::article.article').findMany({
filters: {
rating: {
$between: [1, 20],
},
},
});
strapi.documents().findMany()

$contains

Attribute contains the input value (case-sensitive).

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$contains: 'Hello',
},
},
});
strapi.documents().findMany()

$notContains

Attribute does not contain the input value (case-sensitive).

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$notContains: 'Hello',
},
},
});
strapi.documents().findMany()

$containsi

$containsi is not case-sensitive, while $contains is.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$containsi: 'hello',
},
},
});
strapi.documents().findMany()

$notContainsi

$notContainsi is not case-sensitive, while $notContains is.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$notContainsi: 'hello',
},
},
});
strapi.documents().findMany()

$startsWith

Attribute starts with input value (case-sensitive).

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$startsWith: 'ABCD',
},
},
});
strapi.documents().findMany()

$startsWithi

Attribute starts with input value (case-insensitive).

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$startsWithi: 'ABCD', // will return the same as filtering with 'abcd'
},
},
});
strapi.documents().findMany()

$endsWith

Attribute ends with input value (case-sensitive).

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$endsWith: 'ABCD',
},
},
});
strapi.documents().findMany()

$endsWithi

Attribute ends with input value (case-insensitive).

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$endsWith: 'ABCD', // will return the same as filtering with 'abcd'
},
},
});
strapi.documents().findMany()

$null

Attribute is null. Use true or false: $null: false is equivalent to $notNull: true.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$null: true,
},
},
});
strapi.documents().findMany()

$notNull

Attribute is not null. Use true or false: $notNull: false is equivalent to $null: true.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
title: {
$notNull: true,
},
},
});

Logical operators​

strapi.documents().findMany()

$and

All nested conditions must be true.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
$and: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
strapi.documents().findMany()

$or

One or many nested conditions must be true.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
$or: [
{
title: 'Hello World',
},
{
createdAt: { $gt: '2021-11-17T14:28:25.843Z' },
},
],
},
});
strapi.documents().findMany()

$not

Negates the nested conditions.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
$not: {
title: 'Hello World',
},
},
});
Note

$not can be used as:

  • a logical operator (e.g. in filters: { $not: { // conditions... }})
  • an attribute operator (e.g. in filters: { attribute-name: $not: { ... } }).
Tip

$and, $or and $not operators are nestable inside of another $and, $or or $not operator.

Relation, media field, and component filters​

A relation, media field, or component attribute accepts a filter on the attributes of the related entry, media file, or component (e.g. author: { name: 'John' }). It also accepts exactly one operator applied to its id, as the REST API does.

strapi.documents().findMany()

Operator on the related id

Applies the operator to the id of the related entry, media file, or component.

const entries = await strapi.documents('api::article.article').findMany({
filters: {
// Same as author: { id: { $in: [1, 2] } }
author: { $in: [1, 2] },
},
});

// Articles with a cover
const withCover = await strapi.documents('api::article.article').findMany({
filters: { cover: { $notNull: true } },
});

// Articles with no seo component
const withoutSeo = await strapi.documents('api::article.article').findMany({
filters: { seo: { $null: true } },
});
Caution
  • The operator compares the numeric id, not the documentId. A documentId passed this way matches no entry, or throws an error with PostgreSQL. To filter on documentId, name it: author: { documentId: { $in: ['a1b2c3', 'd4e5f6'] } }.
  • The operator must be the only key. An operator next to other keys (e.g. author: { $in: [1, 2], name: 'John' }) or 2 operators (e.g. author: { $gt: 1, $lt: 5 }) throw an error. Name id instead: author: { id: { $gt: 1, $lt: 5 }, name: 'John' }.
  • The TypeScript filter types do not accept a bare id (e.g. author: 5, write author: { id: 5 } or author: { $eq: 5 }), nor a text operator directly on the attribute (e.g. author: { $contains: '1' }).
  • $ne and $notIn compare the id, so entries without a related entry are not returned.
Was this page helpful?