WHAT YOU'LL LEARN
  • How to use AND and OR conditionals when filtering entries
  • How nested AND / OR queries work
  • How to combine both conditionals in a single query

Overview
anchor

Both AND and OR conditionals are arrays of filter conditions. The available filter keys depend on the fields defined on the model you are querying. All user-defined field filters use the values. prefix (e.g. values.title_contains).

The examples below use the Webiny SDK. The where object is passed directly to sdk.cms.listEntries().

TheANDConditional
anchor

AND requires all conditions in the array to match. It behaves the same as placing filters at the root of where, with the added ability to repeat the same operator multiple times (which is not possible at the root level).

SimpleANDExamples
anchor

Search for Entries Where the Title Contains Both "Headless" and "Cms"
anchor

At the root level you can only use values.title_contains once. Wrapping conditions in AND lets you apply the same filter key multiple times:

Equivalent condition: (values.title contains "headless" AND values.title contains "cms")

Combine a Root-Level Filter WithAND
anchor

Root-level filters and AND are applied together — all must match:

Equivalent condition: (values.category = "cat-id-1" AND values.title contains "headless" AND values.title contains "cms")

ComplexANDExample
anchor

Search for articles that:

  • are in category cat-id-1
  • have both “headless” and “cms” in the title
  • are authored by one of three authors
  • were created in 2022

Equivalent condition: (values.category = "cat-id-1" AND values.title contains "headless" AND values.title contains "cms" AND (values.author in [...] AND createdOn between 2022))

createdOn is a system meta field available on all entries. It does not use the values. prefix.

The same query can be flattened when there is no need for repeated keys:

This produces the same result as the nested version above.

TheORConditional
anchor

OR requires at least one condition in the array to match.

SimpleORExamples
anchor

Search for Entries Where the Title Contains "Headless" or "Cms"
anchor

Equivalent condition: (values.title contains "headless" OR values.title contains "cms")

Multiple Filters Inside OneORBranch
anchor

When an OR branch contains more than one filter, all filters in that branch must match (implicit AND within the branch):

Equivalent condition: ((values.title contains "headless" AND values.category = "cat-id-1") OR values.title contains "cms")

ComplexORExample
anchor

Search for articles that match any of:

  • title contains “headless”
  • title contains “cms”
  • category is cat-id-1 or cat-id-2

Equivalent condition: (values.title contains "headless" OR values.title contains "cms" OR (values.category = "cat-id-1" OR values.category = "cat-id-2"))

MixingANDandOR
anchor

ORat the Root With NestedANDandOR
anchor

Search for articles that match any of:

  • title contains “headless”
  • title contains “cms”
  • title contains both “webiny” and “serverless”, and was created in January 2021 or January 2022

ANDat the Root With NestedORandAND
anchor

Search for articles that match all of:

  • title contains “headless”
  • title contains “cms”
  • title contains “webiny” or “serverless”, or was created in January 2021 or January 2022

ORandANDBoth at the Root Level
anchor

Search for articles that:

  • are written by author author-1 OR are in category cat-id-2
  • AND have both “headless” and “cms” in the title

Equivalent condition: ((values.author = "author-1" OR values.category = "cat-id-2") AND (values.title contains "headless" AND values.title contains "cms"))

AND and OR conditionals can be nested indefinitely, but deep nesting may result in performance issues — particularly on DynamoDB-only deployments. Keep nesting shallow where possible.