MongoDB 术语、安装及依赖
本章我们讨论 MongoDB 的术语、安装及 Rust 操作 MongoDB 的依赖。MongoDB 插入数据
本章我们将讨论 MongoDB 插入数据的方法。MongoDB 查询数据
本章将讨论 MongoDB 查询全部数据、查询单条数据以及分页显示的方法。MongoDB 修改数据
本章将讨论 MongoDB 修改已有数据、替换已有数据、修改或插入新数据的方法。MongoDB 删除记录
本章将讨论 Mongo DB 删除记录操作MongoDB 过滤
本章将讨论 MongoDB 中最常用的功能:过滤器的定义。MongoDB 聚合操作
本章将讨论 MongoDB 的两种聚合操作:单一聚合和聚合管道。
MongoDB 查询数据
- 11
- 2025-06-06 16:33:36
查询全部数据
通过 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
结构体,它提供了一些额外操作:
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"
}
}
}