use bson::Document;
use mongodb::options::FindOptions;
impl super::Database {
pub fn find<R>(
&self,
collection: &str,
filter: Option<Document>,
options: Option<FindOptions>,
) -> Result<Vec<R>, String>
where
for<'r> R: serde::Deserialize<'r>,
{
let collection = self.db.collection(&String::from(collection));
match collection.find(filter, options) {
Ok(cursor) => {
let data: Vec<R> = cursor
.filter_map(|doc_cursor| {
bson::from_bson::<R>(bson::Bson::Document(match doc_cursor {
Ok(doc) => doc,
_ => return None,
}))
.ok()
})
.collect();
Ok(data)
}
Err(e) => return Err(e.to_string()),
}
}
}