add sort
This commit is contained in:
parent
37cca40192
commit
228f113dbf
3 changed files with 21 additions and 24 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
@ -698,7 +698,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "mongod"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"futures",
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "mongod"
|
||||
version = "0.2.0"
|
||||
version = "0.2.1"
|
||||
edition = "2021"
|
||||
|
||||
[features]
|
||||
|
|
|
@ -8,7 +8,10 @@ use serde::de::DeserializeOwned;
|
|||
use serde_json::{Map, Value};
|
||||
use valid::Validate;
|
||||
|
||||
use crate::{cache_read, cache_write, col, collect_results, get_mongo, id_of};
|
||||
#[cfg(feature = "cache")]
|
||||
use crate::{cache_read, cache_write};
|
||||
|
||||
use crate::{col, collect_results, get_mongo, id_of};
|
||||
|
||||
pub mod historic;
|
||||
pub mod reference;
|
||||
|
@ -215,7 +218,7 @@ pub trait Model:
|
|||
/// Get all `Model`s from the database
|
||||
#[must_use]
|
||||
fn find_all() -> impl std::future::Future<Output = Option<Vec<Self>>> {
|
||||
Self::find(doc! {}, None)
|
||||
Self::find(doc! {}, None, None)
|
||||
}
|
||||
|
||||
/// Get all `Model`s partial from the database
|
||||
|
@ -223,7 +226,7 @@ pub trait Model:
|
|||
fn find_all_partial(
|
||||
part: serde_json::Value,
|
||||
) -> impl std::future::Future<Output = Option<Vec<Self::Partial>>> {
|
||||
Self::find_partial(doc! {}, part, None)
|
||||
Self::find_partial(doc! {}, part, None, None)
|
||||
}
|
||||
|
||||
/// Get multiple `Model`s by using a filter from the database. Pass a `limit` parameter to limit the amount of `Model`s returned.
|
||||
|
@ -231,17 +234,15 @@ pub trait Model:
|
|||
fn find(
|
||||
filter: mongodb::bson::Document,
|
||||
limit: Option<i64>,
|
||||
sort: Option<mongodb::bson::Document>,
|
||||
) -> impl std::future::Future<Output = Option<Vec<Self>>> {
|
||||
async move {
|
||||
let db = get_mongo!();
|
||||
let collection = col!(db, Self::collection_name());
|
||||
let mut results = collection
|
||||
.find(
|
||||
filter,
|
||||
limit.map(|x| FindOptions::builder().limit(x).build()),
|
||||
)
|
||||
.await
|
||||
.ok()?;
|
||||
|
||||
let options = FindOptions::builder().limit(limit).sort(sort).build();
|
||||
|
||||
let mut results = collection.find(filter, options).await.ok()?;
|
||||
let docs = collect_results!(results);
|
||||
docs.into_iter()
|
||||
.map(|x| mongodb::bson::from_document(x).unwrap())
|
||||
|
@ -255,6 +256,7 @@ pub trait Model:
|
|||
filter: mongodb::bson::Document,
|
||||
mut part: serde_json::Value,
|
||||
limit: Option<i64>,
|
||||
sort: Option<mongodb::bson::Document>,
|
||||
) -> impl std::future::Future<Output = Option<Vec<Self::Partial>>> {
|
||||
async move {
|
||||
let db = get_mongo!();
|
||||
|
@ -262,18 +264,13 @@ pub trait Model:
|
|||
|
||||
part.as_object_mut()?.insert("_id".into(), 1.into());
|
||||
|
||||
let mut results = collection
|
||||
.find(
|
||||
filter,
|
||||
Some(
|
||||
FindOptions::builder()
|
||||
.projection(Some(mongodb::bson::to_document(&part).unwrap()))
|
||||
.limit(limit)
|
||||
.build(),
|
||||
),
|
||||
)
|
||||
.await
|
||||
.ok()?;
|
||||
let options = FindOptions::builder()
|
||||
.limit(limit)
|
||||
.sort(sort)
|
||||
.projection(Some(mongodb::bson::to_document(&part).unwrap()))
|
||||
.build();
|
||||
|
||||
let mut results = collection.find(filter, Some(options)).await.ok()?;
|
||||
let docs = collect_results!(results);
|
||||
docs.into_iter()
|
||||
.map(|x| mongodb::bson::from_document(x).unwrap())
|
||||
|
|
Loading…
Add table
Reference in a new issue