域名 AXUM.RS 将于 2025 年 10 月到期。我们无意再对其进行续费,如果你有意接续这个域名,请与我们取得联系。
  • AXUM.RS 现仅需人民币 3000 元(大写:叁仟元整。接受适度议价
  • 按照行业规则,AXUM.RS 到期后,大概率会进入长时间的赎回期,该期间内,如果你想拥有该域名,将要付出高额的费用
  • 我们已启用 AXUM.EU.ORG 域名,并将持续运营
  • 仅接受微信或支付宝交易
如果你对 AXUM.RS 有兴趣,请和我们进行联系:

MongoDB 查询数据

查询全部数据

通过 find() 方法,可以对数据进行查询,它的参数:

  • filter 是一个 Document 结构体
    • 可以通过 mongodb::bson::doc! 宏(它其实是重新导出的 bson::doc!)快速构建
    • 它用于指定过滤条件,通过传递一个空的过滤条件,即 doc!{} 可以实现查询全部数据

它的返回值是 Find 结构体,该结构体中包含诸多额外操作,比如排序:

  • sort():排序,它的参数也是一个 Document
    • 语法:{字段名:值}
    • 取值:
      • -1:降序
      • 1:升序

下一小节我们将对其进行更进一步地说明。

测试:

结果:

HTTP/1.1 200 OK
content-type: application/json
content-length: 483
connection: close
date: Wed, 04 Jun 2025 12:38:41 GMT

[
  {
    "_id": "d100i1kdrfaihab7dmjg",
    "title": "note3",
    "content": "笔记3",
    "created_at": {
      "$date": {
        "$numberLong": "1749027078627"
      }
    }
  },
  {
    "_id": "d100i1kdrfaihab7dmj0",
    "title": "note2",
    "content": "笔记2",
    "created_at": {
      "$date": {
        "$numberLong": "1749027078627"
      }
    }
  },
  {
    "_id": "d100i1kdrfaihab7dmig",
    "title": "note1",
    "content": "笔记1",
    "created_at": {
      "$date": {
        "$numberLong": "1749027078627"
      }
    }
  },
  {
    "_id": "d100fo4drfai1th323sg",
    "title": "foo",
    "content": "bar",
    "created_at": {
      "$date": {
        "$numberLong": "1749026784227"
      }
    }
  }
]

分页查询全部数据

上一小节提到,find() 的返回值是一个 Find 结构体,它提供了一些额外操作:

  • skip():跳过指定记录数,它的参数是一个 u64 值,用于指定要跳过的记录数
  • limit():限定查询的记录数,它的参数是一个 i64 值,用于指定要限定的记录数
pub async fn list_pagination(
    State(state): State<ArcAppState>,
    Query(query): Query<PaginationRequest>,
) -> Result<Json<Vec<model::Note>>> {
    let mut notes = vec![];
    let mut cursor = state
        .note_collect()
        .find(doc! {})
        .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);
    }
    Ok(Json(notes))
}


#[derive(Deserialize)]
pub struct PaginationRequest {
    pub page: Option<u32>,
}
impl PaginationRequest {
    pub fn page(&self) -> u32 {
        self.page.unwrap_or(0)
    }

    pub fn limit(&self) -> u32 {
        3
    }
    pub fn offset(&self) -> u32 {
        self.page() * self.limit()
    }
}

测试:

## 分页显示笔记(无参数)
GET http://127.0.0.1:9527/pagination
Content-Type: application/json

## 分页显示笔记,显式第1页
GET http://127.0.0.1:9527/pagination?page=0
Content-Type: application/json

## 分页显示笔记,第2页
GET http://127.0.0.1:9527/pagination?page=1
Content-Type: application/json

结果:

HTTP/1.1 200 OK
content-type: application/json
content-length: 367
connection: close
date: Wed, 04 Jun 2025 12:37:38 GMT

[
  {
    "_id": "d100i1kdrfaihab7dmjg",
    "title": "note3",
    "content": "笔记3",
    "created_at": {
      "$date": {
        "$numberLong": "1749027078627"
      }
    }
  },
  {
    "_id": "d100i1kdrfaihab7dmj0",
    "title": "note2",
    "content": "笔记2",
    "created_at": {
      "$date": {
        "$numberLong": "1749027078627"
      }
    }
  },
  {
    "_id": "d100i1kdrfaihab7dmig",
    "title": "note1",
    "content": "笔记1",
    "created_at": {
      "$date": {
        "$numberLong": "1749027078627"
      }
    }
  }
]

--------------------------------------

HTTP/1.1 200 OK
content-type: application/json
content-length: 117
connection: close
date: Wed, 04 Jun 2025 12:37:58 GMT

[
  {
    "_id": "d100fo4drfai1th323sg",
    "title": "foo",
    "content": "bar",
    "created_at": {
      "$date": {
        "$numberLong": "1749026784227"
      }
    }
  }
]

查询单条

pub async fn get(
    State(state): State<ArcAppState>,
    Path(id): Path<String>,
) -> Result<Json<Option<model::Note>>> {
    let n = state.note_collect().find_one(doc! {"_id":&id}).await?;
    Ok(Json(n))
}

测试:

## 获取笔记
GET http://127.0.0.1:9527/d100i1kdrfaihab7dmjg
Content-Type: application/json

结果:

HTTP/1.1 200 OK
content-type: application/json
content-length: 121
connection: close
date: Fri, 06 Jun 2025 05:57:25 GMT

{
  "_id": "d100i1kdrfaihab7dmjg",
  "title": "note3",
  "content": "笔记3",
  "created_at": {
    "$date": {
      "$numberLong": "1749027078627"
    }
  }
}
要查看完整内容,请先登录