dehaze

Filter query results

You can use where in your queries to filter results based on some field’s values. You can even use multiple filters in the same where clause using and or or.

Example: Fetch details of all fire type pokemons only:

query {
  pokemons(
    where: { type : { _eq: "Fire" }}
  ) @mongo {
    _id
    name
  }
}
const whereClause = cond("type", "==", "Fire")

const { status, data } = await db.get("pokemons")
  .where(whereClause)
  .apply()

Let’s take a look at different operators that can be used to filter results and other advanced use cases:

Equality operators

The equality operators can be used to fetch specific objects based on equality comparison of a field.

For GraphQL, the equality operators are _eq (equal to) and _ne (not equal to). Whereas for the client SDKs, these operators are == and != respectively.

The following are examples of using equality operators on different types.

Example: Integer (Works with Double, Float, Numeric)

Fetch the list of all 3rd evolution pokemons (level is an integer field which indicates the evolution level):

query {
  pokemons(
    where: { level: { _eq: 3}}
  ) @mongo {
    _id
    name
    type
  }
}
const whereClause = cond("level", "==", 3)

const { status, data } = await db.get("pokemons")
  .where(whereClause)
  .apply()

Syntatic Sugar: In GraphQL, you can skip the _eq for equality operator. Example: where: {level: 3}

Example: String or Text

Fetch list of all caught pokemons with name (a text field) as “Pikachu”:

query {
  caught_pokemons (
    where: { name: "Pikachu"}
  ) @mongo {
    _id
    combat_power
  }
}
const whereClause = cond("name", "==", "Pikachu")

const { status, data } = await db.get("caught_pokemons")
  .where(whereClause)
  .apply()

Example: Boolean

Fetch list of all caught pokemons that have been marked favourite by their trainer (is_favourite is a boolean field):

query {
  caught_pokemons(
    where: { is_favourite: true}
  ) @mongo {
    _id
    name
  }
}
const whereClause = cond("is_favourite", "==", true)

const { status, data } = await db.get("caught_pokemons")
  .where(whereClause)
  .apply()

Example: Date (Works with Timestamp as well)

Fetch list of all pokemons that you caught on some special day 😉 (caught_on is a Date field):

query {
  caught_pokemons(
    where: { caught_on: "2019-09-15"}
  ) @mongo {
    _id
    name
  }
}
const whereClause = cond("caught_on", "==", "2019-09-15")

const { status, data } = await db.get("caught_pokemons")
  .where(whereClause)
  .apply()

Comparison operators

The comparison operators can be used to fetch specific objects based on greater than or lesser than comparison of a field.

For GraphQL, the comparison operators are _gt (greater than), _gte (greater than or equal to), _lt (lesser than) and _lte (lesser than or equal to). Whereas for the client SDKs, these operators are >, >=, < and <= respectively.

The following are examples of using these operators on different types.

Example: Integer (Works with Double, Float, Numeric)

Fetch list of all strong pokemons (with combat_power of 2000 or more):

query {
  caught_pokemons(
    where: { combat_power: { _gte: 2000}}
  ) @mongo {
    _id
    name
    combat_power
  }
}
const whereClause = cond("combat_power", ">=", 2000)

const { status, data } = await db.get("todos")
  .where(whereClause)
  .apply()

Example: String or Text

Fetch list of pokemons whose names begin with any letter that comes after P (essentially, a filter based on a dictionary sort):

query {
  pokemons(
    where: { name: { _gt: "P"}}
  ) @mongo {
    _id
    name
  }
}
const whereClause = cond("name", ">", "P")

const { status, data } = await db.get("pokemons")
  .where(whereClause)
  .apply()

Example: Date (Works with Timestamp as well)

Fetch list of all caught pokemons that are caught before a certain date:

query {
  articles(
    where: { caught_on: { _lt: "2019-09-15"}}
  ) @mongo {
    _id
    name
  }
}
const whereClause = cond("caught_on", "<", "2018-09-15")

const { status, data } = await db.get("caught_pokemons")
  .where(whereClause)
  .apply()

List based operators

The list-based operators can be used to compare field values to a list of values.

For GraphQL, the list based operators are _in (in a list) and _nin (not in list). Whereas for the client SDKs, these operators are in and notIn, respectively.

The following are examples of using these operators on different types.

Example: Integer (Works with Double, Float, Numeric)

Fetch a list of all pokemons with level either 1, 2 or 3:

query {
  pokemons(
    where: { level: { _in: [1, 2, 3]}}
  ) @mongo {
    _id
    name
  }
}
const whereClause = cond("level", "in", [1, 2, 3])

const { status, data } = await db.get("pokemons")
  .where(whereClause)
  .apply()

Example: String or Text

Fetch a list of all pokemons that are not of the following type - Water, Fire, Grass:

query {
  pokemons(
    where: { name: { _nin: ["Water", "Fire", "Grass"]}}
  ) @mongo {
    _id
    name
  }
}
const whereClause = cond("name", "notIn", ["Water", "Fire", "Grass"])

const { status, data } = await db.get("pokemons")
  .where(whereClause)
  .apply()

Using multiple filters in the same query

You can group multiple parameters in the same where clause using the logical AND and OR operations. These logical operations can be infinitely nested to apply complex filters.

For GraphQL, _and and _or are the logical AND and OR operators respectively.

Whereas for the client SDKs, and and or are the methods used for AND and OR operations respectively.

Example: AND

Let’s say we want to fetch all pokemons that are between two particular dates by Ash (trainer_id - 1). This is how you would do it:

query {
  caught_pokemons(
    where: {
      _and: {
        "caught_on": {
          _gte: "2019-06-01",
          _lte: "2019-09-15"
        },
        "trainer_id": "1"
      }
    } 
  ) @mongo {
    _id
    name
  }
}
const whereClause = and(
  cond("caught_on", ">=", "2019-06-01"),
  cond("caught_on", "<=", "2019-09-15"),
  cond("trainer_id", "==", "1")
)

const { status, data } = await db.get("caught_pokemons")
  .where(whereClause)
  .apply()

Example: OR

Let’s say we want to fetch information of all Fire-type or Legendary pokemons. This is how you would do it:

query {
  pokemons(
    where: {
      _or: [
        {type: "Fire"},
        {is_legendary: true}
      ]
    } 
  ) @mongo {
    _id
    name
  }
}
const whereClause = or(
  cond("type", "==", "Fire"),
  cond("is_legendary", "==", true)
)

const { status, data } = await db.get("pokemons")
  .where(whereClause)
  .apply()

Filter nested queries

The where argument can be used to filter even nested queries.

Let’s say we want to fetch a list of all the trainers who have joined the game after a particular date along with their Fire-type pokemons. This is how you would do it:

query {
  trainers(
    where: {joined_on: "2019-09-15"}
  ) @mongo {
    _id
    name
    caught_pokemons(
      where: {
        _and: {
          trainer_id: "trainers.id"
          type: "Fire"
        }
      }
    ) @mongo {
      _id
      name
    }
  }
}

Have a technical question?

Improve the docs!