I'm trying to create a method to which I can pass a mongodb connection pool, an objectId and the collection name to retrieve data.
I came up with the code below but which doesn't compile because of the following error:
error: the method
find_oneexists for structmongodb::Collection<T>, but its trait bounds were not satisfied label: method cannot be called onmongodb::Collection<T>due to unsatisfied trait bounds note: the following trait bounds were not satisfied:T: DeserializeOwnedT: UnpinT: std::marker::SendT: Synclabel: method cannot be called onmongodb::Collection<T>due to unsatisfied trait bounds
What I'm I doing wrong?
pub async fn generic_find_by_id<T>(db: &AppContext, object_id: String, collection_name: &str) -> Option<T> {
let collection = db.mongodb_pool.collection::<T>(collection_name);
let id_obj = ObjectId::parse_str(object_id);
let found = match id_obj {
Ok(id) => {
let filter = doc! {"_id": id};
let result = collection.find_one(filter, None).await;
match result {
Ok(result) => {
match result {
Some(result) => {
return Some(result);
}
None => {
return None;
}
}
}
Err(_) => {
return None;
}
}
}
Err(_) => {
return None;
}
};
}