async fn get_champions_by_role(
role: &str,
collection: &Collection<ChampionData>,
) -> Result<Vec<ChampionData>>Expand description
⚙️ Retrieves all champions that match the specified role from the database.
This asynchronous function queries a MongoDB collection to find all champions that include the specified role.
The function returns a vector of ChampionData objects that match the role provided.
§Parameters:
role: A reference to a string representing the role to filter champions by (e.g., “Top”, “Jungler”).collection: A reference to a MongoDB collection ofChampionDatarepresenting the champion data stored in the database.
§Returns:
Result<Vec<ChampionData>, mongodb::error::Error>: On success, returns a vector ofChampionDataobjects that match the role. On failure, returns anErrorfrom MongoDB indicating what went wrong.
§Example:
let role = "AD Carry";
let champions = get_champions_by_role(role, &collection).await?;
for champion in champions {
println!("Champion: {}", champion.name);
}§⚠️ Notes:
- The function uses MongoDB’s
$inoperator to search for champions whose roles include the specified value. - The result is collected into a vector using the
try_collectmethod for convenience.
§Related Functions:
match_role_with_database_roles: Converts aRoleenum into the corresponding string representation, which can be used as input for this function.
§Dependencies:
- This function depends on a MongoDB collection that stores
ChampionDatadocuments. - Requires the
futurescrate for thetry_collectmethod to handle the cursor results asynchronously.