对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

快速参考事务语法示例

在此页面上,您可以查看使用 Rust 驱动程序执行多个常见 MongoDB 任务的示例。 下表的每一行都描述了该任务,显示了执行该任务的驱动程序语法,并包含指向相关参考和 API 文档的链接。

Rust 驱动程序提供用于执行异步应用程序的异步运行时。 此外,该驱动程序支持阻塞同步运行时。 对于下表中列出的每个 MongoDB 任务,您可以查看使用异步和同步 API 的示例。

提示

如需了解异步和同步运行时相关详情,请参阅《异步和同步 API 指南》。

命令
语法

异步运行时

let result = collection.find_one(doc! { "title": "Peter Pan" }).await?;

同步运行时

let result = collection.find_one(doc! { "title": "Peter Pan" }).run()?;

查找多个文档

API 文档
查找文档

异步运行时

let filter = doc! { "year": 1925 };
let mut cursor = collection.find(filter).await?;

同步运行时

let filter = doc! { "year": 1925 };
let mut cursor = collection.find(filter).run()?;

异步运行时

let doc = doc! {
"title": "Mistress America", "type": "movie"
};
let result = collection.insert_one(doc).await?;

同步运行时

let doc = doc! {
"title": "Mistress America", "type": "movie"
};
let result = collection.insert_one(doc).run()?;

异步运行时

let docs = vec![
doc! { "title": "Friends With Money", "runtime": 88 },
doc! { "title": "Please Give", "runtime": 90 },
doc! { "title": "You Hurt My Feelings", "runtime": 93 },
];
let result = collection.insert_many(docs).await?;

同步运行时

let docs = vec![
doc! { "title": "Friends With Money", "runtime": 88 },
doc! { "title": "Please Give", "runtime": 90 },
doc! { "title": "You Hurt My Feelings", "runtime": 93 },
];
let result = collection.insert_many(docs).run()?;

异步运行时

let filter = doc! { "title": "Burn After Reading"};
let update = doc! {
"$set": doc!{ "num_mflix_comments": 1 }
};
let result = collection.update_one(filter, update).await?;

同步运行时

let filter = doc! { "title": "Burn After Reading"};
let update = doc! {
"$set": doc!{ "num_mflix_comments": 1 }
};
let result = collection.update_one(filter, update).run()?;

异步运行时

let filter = doc! { "rated": "PASSED"};
let update = doc! {
"$set": doc!{ "rated": "Not Rated" }
};
let result = collection.update_many(filter, update).await?;

同步运行时

let filter = doc! { "rated": "PASSED"};
let update = doc! {
"$set": doc!{ "rated": "Not Rated" }
};
let result = collection.update_many(filter, update).run()?;

异步运行时

let filter = doc! { "title": "è Nous la Libertè" };
let replacement = doc! {
"title": "À nous la liberté",
"type": "movie",
"directors": vec! [ "René Clair" ]
};
let result = collection.replace_one(filter, replacement).await?;

同步运行时

let filter = doc! { "title": "è Nous la Libertè" };
let replacement = doc! {
"title": "À nous la liberté",
"type": "movie",
"directors": vec! [ "René Clair" ]
};
let result = collection.replace_one(filter, replacement).run()?;

异步运行时

let filter = doc! { "title": "Search and Destroy" };
let result = collection.delete_one(filter).await?;

同步运行时

let filter = doc! { "title": "Search and Destroy" };
let result = collection.delete_one(filter).run()?;

异步运行时

let filter = doc! {
"year": doc! { "$lt": 1920 }
};
let result = collection.delete_many(filter).await?;

同步运行时

let filter = doc! {
"year": doc! { "$lt": 1920 }
};
let result = collection.delete_many(filter).run()?;

从游标迭代访问数据

API 文档
从游标访问数据指南

异步运行时

let mut cursor = collection
.find(doc! { "$and": vec!
[
doc! { "metacritic": doc! { "$gt": 90 } },
doc! { "directors": vec! [ "Martin Scorsese" ] }
] })
.await?;
while let Some(result) = cursor.try_next().await? {
println!("{}", result);
}

同步运行时

let cursor = collection
.find(doc! { "$and": vec!
[
doc! { "metacritic": doc! { "$gt": 90 } },
doc! { "directors": vec! [ "Martin Scorsese" ] }
] })
.run()?;
for result in cursor {
println!("{}", result?);
}

以数组形式访问游标中的数据

API 文档
从游标访问数据指南

异步运行时

let cursor = collection.find(doc! { "title": "Secrets & Lies" }).await?;
let results: Vec<Document> = cursor.try_collect().await?;

同步运行时

let cursor = collection.find(doc! { "title": "Secrets & Lies" }).run()?;
let results: Vec<Result<Document>> = cursor.collect();

异步运行时

let filter = doc! {
"languages": vec! [ "Mandarin" ]
};
let result = collection.count_documents(filter).await?;

同步运行时

let filter = doc! {
"languages": vec! [ "Mandarin" ]
};
let result = collection.count_documents(filter).run()?;

列出字段的不同值

API 文档
非重复字段值指南

异步运行时

let field_name = "title";
let filter = doc! {
"directors": vec! [ "Sean Baker" ]
};
let results = collection.distinct(field_name, filter).await?;

同步运行时

let field_name = "title";
let filter = doc! {
"directors": vec! [ "Sean Baker" ]
};
let results = collection.distinct(field_name, filter).run()?;

限制检索的文档数量

API 文档
指定文档指南

异步运行时

let filter = doc! { "awards.wins": 25};
let mut cursor = collection.find(filter).limit(5).await?;

同步运行时

let filter = doc! { "awards.wins": 25};
let mut cursor = collection.find(filter).limit(5).run()?;

跳过检索到的文档

API 文档
指定文档指南

异步运行时

let filter = doc! { "runtime": 100 };
let mut cursor = collection.find(filter).skip(1).await?;

同步运行时

let filter = doc! { "runtime": 100 };
let mut cursor = collection.find(filter).skip(1).run()?;

检索文档时对文档进行排序

API 文档
指定文档指南

异步运行时

let filter = doc! {
"directors": vec! [ "Nicole Holofcener" ]
};
let mut cursor = collection
.find(filter)
.sort(doc! { "imdb.rating": 1 })
.await?;

同步运行时

let filter = doc! {
"directors": vec! [ "Nicole Holofcener" ]
};
let mut cursor = collection
.find(filter)
.sort(doc! { "imdb.rating": 1 })
.run()?;

检索项目文档字段时

API 文档
指定字段指南

异步运行时

let filter = doc! { "year": 2015 };
let mut cursor = collection
.find(filter)
.projection(doc! { "title": 1, "metacritic": 1, "_id": 0 })
.await?;

同步运行时

let filter = doc! { "year": 2015 };
let mut cursor = collection
.find(filter)
.projection(doc! { "title": 1, "metacritic": 1, "_id": 0 })
.run()?;

异步运行时

let index: IndexModel = IndexModel::builder().keys(doc! { "title": 1 }).build();
let result = collection.create_index(index).await?;

同步运行时

let index: IndexModel = IndexModel::builder().keys(doc! { "title": 1 }).build();
let result = collection.create_index(index).run()?;