Skip to content

DB sort by method #490

@xelab04

Description

@xelab04

I am querying a db table with many rows. I want to filter by a field, then get the 5 objects with the greatest values. I was thinking something like this, to get the 5 oldest people above the age of 18:

#[model]
pub struct person {
    #[model(primary_key)]
    pub id: Auto<i32>,
    pub age: i32
}
// ...
let mut oldest_five = Query::<Article>::new();
let oldest_five = oldest_five
    .filter(Expr::gt( Expr::field("age"), Expr::value(18)  ))
    .order(Expr::asc( Expr::field("age") ))
    .limit(5)
    .all(db)
    .await?

If I had to get all fields, then sort them within the Rust code and not the db query, I could risk high RAM usage. Also with the risk of less readable/fluent code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions