Here, I am using MongoDB to get the collections:
let filter = doc! {};
let mut cursor = match collection.find(filter).await {
Ok(cursor) => cursor,
Err(e) => {
eprintln!("Error querying MongoDB: {}", e);
return HttpResponse::InternalServerError().body("Error querying MongoDB");
}
};
I would like to add a short by createdAt column base:
let sort = doc! { "createdAt ": -1 };
Please give a solution.
In the mongodb v3.0 crate, many commands use a builder pattern for additional options. You'll see that .find()
returns a Find
type which includes additional methods you can call before .await
-ing the result. In this case you can simply use the .sort()
method:
let filter = doc! {};
let sort = doc! { "createdAt ": -1 };
let mut cursor = match collection.find(filter).sort(sort).await {
... // ^^^^^^^^^^^
};