1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use mongodb::coll::options::AggregateOptions;
use mongodb::db::ThreadedDatabase;
use bson::Document;

impl super::Database {

    pub fn aggregate<R>(
        &self,
        collection: String,
        pipeline: Vec<Document>,
        options: Option<AggregateOptions>,
    ) -> Result<Vec<R>, String>
    where
        for <'r> R: serde::Deserialize<'r>,
    {
        let collection = self.db.get().unwrap().collection(&collection);
        match collection
            .aggregate(
                pipeline,
                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()),
        }
    }
}