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.
Example: Sort all the trainers by their name:
query {
trainers(
sort: ["name"]
) @mongo {
id
name
}
}
const { status, data } = await db.get("trainers")
.sort("name")
.apply()
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"]
) @mongo {
id
name
pokemons(
sort: ["-combat_power]
) @mongo {
name
combat_power
}
}
}
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", "-caught_on"]
) @mongo {
id
name
caught_on
combat_power
}
}
const { status, data } = await db.get("caught_pokemons")
.sort("name", "-caught_on")
.apply()