Connect

Filter Rules

Learn about filter rules in Directus - available operators, filter syntax, relational fields, dynamic variables, logical operators, and functions parameters. Understand how to build complex filters for permissions, validations, and automations.

Filters are used in permissions, validations, and automations, as well as throughout the APIs and in extensions. All filters use standard syntax and operators which are described on this page.

Available Operators

OperatorDescription
_eqEquals
_neqDoesn't equal
_ltLess than
_lteLess than or equal to
_gtGreater than
_gteGreater than or equal to
_inIs one of
_ninIs not one of
_nullIs null
_nnullIsn't null
_containsContains
_icontainsContains (case-insensitive)
_ncontainsDoesn't contain
_starts_withStarts with
_istarts_withStarts with (case-insensitive)
_nstarts_withDoesn't start with
_nistarts_withDoesn't start with (case-insensitive)
_ends_withEnds with
_iends_withEnds with (case-insensitive)
_nends_withDoesn't end with
_niends_withDoesn't end with (case-insensitive)
_betweenIs between two values (inclusive)
_nbetweenIs not between two values (inclusive)
_emptyIs empty (null or falsy)
_nemptyIsn't empty (null or falsy)
_intersects 1Intersects a point
_nintersects 1Doesn't intersect a point
_intersects_bbox 1Intersects a bounding box
_nintersects_bbox 1Doesn't intersect a bounding box
_regex 2Regular expression (escape backslashes)
_some 3At least one related value is true
_none 3No related values are true

1 Only available on geometry fields.
2 Only available in validation permissions.
3 Only available on One to Many relationship fields.

Filter Syntax

{
  "field": {
    "operator": "value"
  }
}

The field can exist on the current collection or a relational collection.

The operator must be any valid filter operator such as 'equals' or 'contains'.

The value can be any fixed static value or one of the provided dynamic variables.

Example
This filter checks the title field contains the case-sensitive substring 'Directus':
{
  "title": {
    "_contains": "Directus"
  }
}

Relational Fields

You can specify related values by nesting field names. For example, if you have a relational Many to One author field, and want to specify a filter on the author's name field:

{
  "authors": {
    "authors_id": {
      "name": {
        "_eq": "Rijk van Zanten"
      }
    }
  }
}

When applying filters to a One to Many field, Directus will default to a 'some' search.

Example
This filter checks all related category names and will check is at least one value is equal to 'Recipe':
{
  "categories": {
    "name": {
      "_eq": "Recipe"
    }
  }
}

This behavior can be overridden by using the explicit _some and _none operators.

Example
{
  "categories": {
    "_none": {
      "name": {
        "_eq": "Recipe"
      }
    }
  }
}

Dynamic Variables

VariableDescription
$CURRENT_USERThe primary key of the currently authenticated user.
$CURRENT_ROLEThe primary key of the role for the currently authenticated user
$NOWThe current timestamp
$NOW(<adjustment>)The current timestamp plus/minus a given distance, for example $NOW(-1 year), $NOW(+2 hours)
Examples
{
  "owner": {
    "_eq": "$CURRENT_USER"
  }
}
{
  "datetime": {
    "_lte": "$NOW"
  }
}
Nested user and role variables in permissions
When configuring permissions, $CURRENT_USER and $CURRENT_ROLE allow you to specify any related field, such as $CURRENT_ROLE.name or $CURRENT_USER.avatar.filesize.

Logical Operators

You can group multiple rules using the _and or _or logical operators. Each logical operator holds an array of filter rules. Logical operators can be nested directly inside of each other, but not inside of other filter rules.

{
  "_and": [
    {
      "field": {
        "operator": "value"
      }
    },
    {
      "field": {
        "operator": "value"
      }
    }
  ]
}
Example
{
  "_or": [
    {
      "_and": [
        {
          "user_created": {
            "_eq": "$CURRENT_USER"
          }
        },
        {
          "status": {
            "_in": ["published", "draft"]
          }
        }
      ]
    },
    {
      "_and": [
        {
          "user_created": {
            "_neq": "$CURRENT_USER"
          }
        },
        {
          "status": {
            "_in": ["published"]
          }
        }
      ]
    }
  ]
}

Functions Parameters

Functions accept a field and return a modified value. Functions can be used in any query parameter you'd normally supply a field key, including fields, aggregation, and filters.

The syntax for using a function is function(field).

FunctionDescription
yearExtract the year from a datetime/date/timestamp field
monthExtract the month from a datetime/date/timestamp field
weekExtract the week from a datetime/date/timestamp field
dayExtract the day from a datetime/date/timestamp field
weekdayExtract the weekday from a datetime/date/timestamp field
hourExtract the hour from a datetime/date/timestamp field
minuteExtract the minute from a datetime/date/timestamp field
secondExtract the second from a datetime/date/timestamp field
countExtract the number of items from a JSON array or relational field
Example
{
  _and: [
    {
      "year(published_date)": {
        _eq: 1968,
      },
    },
    {
      "month(published_date)": {
        _eq: 4,
      },
    },
  ],
},