dehaze

Sort query results

You can sort your query results via Space Cloud by using the sort argument. The sort argument can be used to sort nested queries too.

The value of sort argument should be an array containing the name of fields to sort the results by.

The default order of sorting for any field provided in the sort array is ascending. To specify descending order, just put a minus (-) sign before the field name.

Sorting simple queries

Example: Sort all the trainers by their name:

query {
  trainers(
    sort: {name: 1}
  ) @mongo {
    id
    name
  }
}

Sorting nested queries

Example: Sort all the trainers by their name in ascending order and their pokemons by their combat_power in descending order:

query {
  trainers(
    sort: { name: 1 }
  ) @mongo {
    id
    name
    pokemons(
      sort: { combat_power : -1 }
    ) @mongo {
      name
      combat_power
    }
  }
}

Sorting by multiple fields

Example: Sort all caught pokemons first by their name in ascending order and then by their caught_on date in descending order:

query {
  caught_pokemons(
    sort: { name: 1, caught_on: -1 }
  ) @mongo {
    id
    name
    caught_on
    combat_power
  }
}
const { status, data } = await db.get("caught_pokemons")
  .sort("name", "-caught_on")
  .apply()

Have a technical question?

Improve the docs!