Filtering data

Each collection in the GraphQL schema has a filter argument which can be used to filter the results. You can filter by any custom field. Each field type supports different operators.

The syntax for filter is based on the mongodb query syntax.

String fields

OperatorDescription
eq(equal)Find nodes with field of (strict) equality.
ne(not equal)Find nodes with field not equal to provided value.
inFind nodes with field matching any of the provided values.
nin(not in)Find nodes with field not matching any of the provided values.
regexFilter nodes with property matching provided regular expression.
len(length)Filter nodes which have a string field of specified length.
query {
  allPost(filter: { id: { in: ["1", "2"] }}) {
    edges {
      node {
        title
      }
    }
  }
}

This example will query nodes where id is 1 or 2.

Date fields

OperatorDescription
gt(greater than)Find nodes with field greater than provided value.
gte(greater or equal)Find nodes with field greater or equal to provided value.
lt(less than)Find nodes with field less than provided value.
lte(less than or equal)Find nodes with field less than or equal to provided value.
dteq(equal dates)Filter nodes by date property equal to provided date value.
betweenFind nodes with field value between provided values.
query {
  allPost(filter: { date: { gte: "2017" }}) {
    edges {
      node {
        title
      }
    }
  }
}

This example will query only nodes where date is greater than or equal to 2017.

Boolean fields

OperatorDescription
eq(equal)Find nodes with field of (strict) equality.
ne(not equal)Find nodes with field not equal to provided value.
inFind nodes with field matching any of the provided values.
nin(not in)Find nodes with field not matching any of the provided values.
query {
  allPost(filter: { featured: { eq: true }}) {
    edges {
      node {
        title
        featured
      }
    }
  }
}

This example will query only nodes where featured is true.

Number fields

OperatorDescription
eq(equal)Find nodes with field of (strict) equality.
ne(not equal)Find nodes with field not equal to provided value.
inFind nodes with field matching any of the provided values.
nin(not in)Find nodes with field not matching any of the provided values.
gt(greater than)Find nodes with field greater than provided value.
gte(greater or equal)Find nodes with field greater or equal to provided value.
lt(less than)Find nodes with field less than provided value.
lte(less than or equal)Find nodes with field less than or equal to provided value.
betweenFind nodes with field value between provided values.
query {
  allProduct(filter: { price: { between: [49, 99] }}) {
    edges {
      node {
        title
        price
      }
    }
  }
}

This example will query only nodes with price value between 49 and 99.

Array fields

OperatorDescription
sizeFilter nodes which have an array property of specified size.
containsFind nodes with field containing the provided value.
containsAnyFind nodes with field containing any of the provided values.
containsNoneFind nodes with field containing none of the provided values.
query {
  allPost(filter: { keywords: { contains: ["gridsome"] }}) {
    edges {
      node {
        title
        keywords
      }
    }
  }
}

This example will query only nodes which has the gridsomekeyword.

Edit this page on GitHub