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 operatorslink

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)link

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 Textlink

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: Booleanlink

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)link

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 operatorslink

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)link

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 Textlink

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)link

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()

Search operator (Regex based)link

The regex based search operator in Space Cloud is used to compare field values to a particular regex pattern.

Example: Make an case insensitive search for all pokemons that have word strong in their description:

query {
  pokemons(
    where: { description: { _regex: "(?i)strong"}}
  ) @mongo {
    _id
    name
  }
}
const whereClause = cond("description", "regex", "(?i)strong")

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

Note: The regex operator is mapped to the ~ operator in Postgres, $regex operator in MongoDB and REGEXP in MySQL.

List based operatorslink

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)link

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 Textlink

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()

JSON operatorslink

The _contains operator is used to filter based on JSON columns.

Example: _containslink

Fetch all pokemons with a particular combat_power (present in stats JSON column):

query {
  pokemons(
    where: { stats: { _contains: $jsonFilter}}
  ) @mongo {
    _id
    name
    stats {
      combat_power
    }
  }
}

with variables:

{
  "jsonFilter": {
    "combat_power": 500
  }
}
const whereClause = cond("stats", "contains", {combat_power: 500})

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

Using multiple filters in the same querylink

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.

Example: ANDlink

By default a logical AND operation is performed on all the conditions mentioned in the where clause.

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

query {
  caught_pokemons(
    where: {
      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: ORlink

To perform a logical OR operation, we have to use the _or operator in GraphQL.

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 querieslink

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: {
        trainer_id: "trainers.id"
        type: "Fire"
      }
    ) @mongo {
      _id
      name
    }
  }
}

Have a technical question?

Improve the docs!