MongoDB 聚合操作

本章将讨论 MongoDB 的两种聚合操作:单一聚合和聚合管道。

单一聚合

我们在做分页时缺少了一个很重要的东西:统计符合条件的记录数。和关系型数据库类似,MongoDB也提供了 count 聚合函数。

count 由 Collection 提供的 count_documents() 方法实现。

我们来看一下修改后的分页:

#[derive(Serialize)]
pub struct Pagination<T> {
    pub page: u32,
    pub page_size: u32,
    pub total: u64,
    pub total_page: u64,
    pub data: Vec<T>,
}
impl<T> Pagination<T> {
    fn new(page: u32, page_size: u32, total: u64, data: Vec<T>) -> Self {
        let total_page = (total as f64 / page_size as f64).ceil() as u64;
        Self {
            page,
            page_size,
            total,
            total_page,
            data,
        }
    }
}
pub async fn list_pagination(
    State(state): State<ArcAppState>,
    Query(query): Query<PaginationRequest>,
) -> Result<Json<Pagination<model::Note>>> {
    let filter = doc! {};
    let count = state.note_collect().count_documents(filter.clone()).await?;
    let mut notes = vec![];
    let mut cursor = state
        .note_collect()
        .find(filter)
        .sort(doc! { "_id": -1})
        .skip(query.offset() as u64)
        .limit(query.limit() as i64)
        .await?;
    while cursor.advance().await? {
        let n = cursor.deserialize_current()?;
        notes.push(n);
    }
    let p = Pagination::new(query.page(), query.limit(), count, notes);
    Ok(Json(p))
}

聚合管道就是将多个聚合操作合并到一个管道中,以便在同一次查询中进行操作。聚合管道非常实用,但本专题用不上这些知识,所以可以自行在官网上查看文档。

要查看完整内容,请先登录