复合运算符
Overview
在本指南中,您可以了解如何使用 Rust 驱动程序执行复合操作。
复合操作将读取和写入操作的功能组合成一个原子动作。 如果您按顺序执行读取操作和写入操作,有人可能会在操作之间更改您的目标文档,从而导致意外结果。 当您执行复合操作时, MongoDB会在您正在修改的文档上放置写锁(write lock),直到操作完成,从而防止中间数据更改。
您可以使用驱动程序执行以下复合操作:
查找并删除一个文档
查找并更新一个文档
查找并替换一个文档
本指南包括以下部分:
示例的样本数据显示复合操作示例使用的样本数据
查找和删除文档描述了如何在单个操作中查找和删除文档
查找和更新文档描述了如何在单个操作中查找和更新文档
查找和替换文档描述了如何在单个操作中查找和替换文档
附加信息提供了本指南中提到的类型和方法的资源和 API 文档链接
提示
要学习;了解如何一次对多个文档执行原子写入操作,请参阅事务指南。
示例样本数据
本指南中的示例使用以下样本文档。 每个文档代表一名学生,并包含有关他们的年龄和就读学校的信息:
{ "name": "Alex Johnson", "age": 8, "school": "Lakeside Elementary" }, { "name": "Samara Khan", "age": 11, "school": "Rolling Hills Middle School" }, { "name": "Ben Joseph", "age": 16, "school": "Aurora High School" }, { "name": "Deanna Porowski", "age": 10, "school": "Lakeside Elementary" }
查找和删除文档
find_one_and_delete()
方法查找并删除与指定查询筛选器匹配的第一个文档。 如果文档与筛选条件匹配,该方法将返回Some
类型。 如果没有匹配的文档,则返回None
类型。
注意
如果要在查找和删除文档之间执行其他操作,可以调用find_one()
方法,然后调用delete_one()
方法。
修改查找和删除行为
您可以选择通过将选项构建器方法链接到find_one_and_delete()
来修改find_one_and_delete()
方法的行为。 这些构建器方法设立FindOneAndDeleteOptions
结构体字段。
下表描述了FindOneAndDeleteOptions
中可用的选项:
选项 | 说明 |
---|---|
| The maximum amount of time in milliseconds that the query can
run. Type: Duration |
| The projection to use when returning results. Type: Document Default: None |
| The sorting order to use when returning results. By default, the driver
returns documents in their natural order, or as they appear in
the database. To learn more, see natural order
in the Server manual glossary. Type: Document Default: None |
| The write concern for the operation. If you don't set this
option, the operation inherits the write concern set for
the collection. To learn more about write concerns, see
Write Concern in the
Server manual. Type: WriteConcern |
| The collation to use when sorting results. To learn more about collations,
see the Collations guide. Type: Collation Default: None |
| The index to use for the operation. To learn more about
indexes, see Indexes in the Server
manual. This option is available only when connecting to
MongoDB Server versions 4.4 and later. Type: Hint Default: None |
| A map of parameters and values. You can access these parameters
as variables in aggregation expressions. This option is available
only when connecting to MongoDB Server versions 5.0 and later. Type: Document |
| An arbitrary Bson value tied to the operation to trace
it through the database profiler, currentOp , and
logs. This option is available only when connecting to
MongoDB Server versions 4.4 and later.Type: Bson Default: None |
注意
设置选项
您可以通过将选项构建器方法直接链接到find_one_and_delete()
方法调用来设立FindOneAndDeleteOptions
字段。 如果使用的是早期版本的驾驶员,则必须通过将选项构建器方法链接到builder()
方法来构造FindOneAndDeleteOptions
实例。 然后,将选项实例作为参数传递给find_one_and_delete()
。
以下代码展示了如何通过将comment()
方法链接到find_one_and_delete()
方法来设立comment
字段:
let res = my_coll.find_one_and_delete(filter) .comment(bson!("hello")) .await?;
查找和删除示例
以下示例使用find_one_and_delete()
方法匹配并删除age
字段的值小于或等于10
的第一个文档:
let filter = doc! { "age": doc! { "$lte": 10 } }; let res = my_coll.find_one_and_delete(filter).await?; println!("Deleted document:\n{:?}", res);
Deleted document: Some(Document({"_id": ObjectId("..."), "name": String("Deanna Porowski"), "age": Int32(10), "school": String("Lakeside Elementary")}))
查找并更新文档
find_one_and_update()
方法查找并更新与指定查询筛选器匹配的第一个文档。 该操作根据您在更新文档中提供的规范来更新文档。 如果文档与筛选条件匹配,该方法将返回Some
类型。 如果没有匹配的文档,则返回None
类型。
注意
如果要在查找和更新文档之间执行其他操作,可以调用find_one()
方法,然后调用update_one()
方法。
修改查找和更新行为
您可以选择通过将选项构建器方法链接到find_one_and_update()
来修改find_one_and_update()
方法的行为。 这些构建器方法设立FindOneAndUpdateOptions
结构体字段。
下表描述了FindOneAndUpdateOptions
中可用的选项:
选项 | 说明 |
---|---|
| The set of filters specifying the array elements to which the
update applies. Type: Vec<Document> |
| If true , allows the driver to perform a write that violates
document-level validation. To learn more about validation, see
Schema Validation in the Server manual.Type: bool Default: false |
| The maximum amount of time in milliseconds that the query can
run. Type: Duration |
| The projection to use when returning results. Type: Document Default: None |
| If Before , the operation returns the document before the
update. If After , the operation returns the updated document.Type: ReturnDocument Default: ReturnDocument::Before |
| The sorting order to use when returning results. By default, the driver
returns documents in their natural order, or as they appear in
the database. To learn more, see natural order
in the Server manual glossary. Type: Document Default: None |
| If true, the operation inserts a document if no documents match
the query filter. Type: bool Default: false |
| The write concern for the operation. If you don't set this
option, the operation inherits the write concern set for
the collection. To learn more about write concerns, see
Write Concern in the
Server manual. Type: WriteConcern |
| The collation to use when sorting results. To learn more about collations,
see the Collations guide. Type: Collation Default: None |
| The index to use for the operation. To learn more about
indexes, see Indexes in the Server
manual. This option is available only when connecting to
MongoDB Server versions 4.4 and later. Type: Hint Default: None |
| A map of parameters and values. You can access these parameters
as variables in aggregation expressions. This option is available
only when connecting to MongoDB Server versions 5.0 and later. Type: Document |
| An arbitrary Bson value tied to the operation to trace
it through the database profiler, currentOp , and
logs. This option is available only when connecting to
MongoDB Server versions 4.4 and later.Type: Bson Default: None |
以下代码展示了如何通过将comment()
方法链接到find_one_and_update()
方法来设立comment
字段:
let res = my_coll.find_one_and_update(filter, update) .comment(bson!("hello")) .await?;
查找和更新示例
此示例演示如何执行以下操作:
调用
find_one_and_update()
方法将查询过滤传递给
find_one_and_update()
,以匹配school
值为"Aurora High School"
的文档将更新文档传递给
find_one_and_update()
,将school
字段设置为"Durango High School"
,并将age
字段递增1
将
return_document()
方法链接到find_one_and_update()
以在更新后返回匹配的文档
let filter = doc! { "school": "Aurora High School" }; let update = doc! { "$set": doc! { "school": "Durango High School" }, "$inc": doc! { "age": 1 } }; let res = my_coll.find_one_and_update(filter, update) .return_document(ReturnDocument::After) .await?; println!("Updated document:\n{:?}", res);
Updated document: Some(Document({"_id": ObjectId("..."), "name": String("Ben Joseph"), "age": Int32(17), "school": String("Durango High School")}))
查找和替换文档
find_one_and_replace()
方法查找并替换与指定查询筛选器匹配的第一个文档。 此操作会将文档中除_id
字段之外的所有字段替换为您提供的字段和值。 如果文档与筛选条件匹配,该方法将返回Some
类型。 如果没有匹配的文档,则返回None
类型。
注意
如果要在查找和替换文档之间执行其他操作,可以调用find_one()
方法,然后调用replace_one()
方法。
修改查找和替换行为
您可以选择通过将选项构建器方法链接到find_one_and_replace()
来修改find_one_and_replace()
方法的行为。 这些构建器方法设立FindOneAndReplaceOptions
结构体字段。
下表描述了FindOneAndReplaceOptions
中可用的选项:
选项 | 说明 |
---|---|
| If true , allows the driver to perform a write that violates
document-level validation. To learn more about validation, see
Schema Validation in the Server manual.Type: bool Default: false |
| The maximum amount of time in milliseconds that the query can
run. Type: Duration |
| The projection to use when returning results. Type: Document Default: None |
| If Before , the operation returns the document before the
update. If After , the operation returns the updated document.Type: ReturnDocument Default: ReturnDocument::Before |
| The sorting order to use when returning results. By default, the driver
returns documents in their natural order, or as they appear in
the database. To learn more, see natural order
in the Server manual glossary. Type: Document Default: None |
| If true, the operation inserts a document if no documents match
the query filter. Type: bool Default: false |
| The write concern for the operation. If you don't set this
option, the operation inherits the write concern set for
the collection. To learn more about write concerns, see
Write Concern in the
Server manual. Type: WriteConcern |
| The collation to use when sorting results. To learn more about collations,
see the Collations guide. Type: Collation Default: None |
| The index to use for the operation. To learn more about
indexes, see Indexes in the Server
manual. This option is available only when connecting to
MongoDB Server versions 4.4 and later. Type: Hint Default: None |
| A map of parameters and values. You can access these parameters
as variables in aggregation expressions. This option is available
only when connecting to MongoDB Server versions 5.0 and later. Type: Document |
| An arbitrary Bson value tied to the operation to trace
it through the database profiler, currentOp , and
logs. This option is available only when connecting to
MongoDB Server versions 4.4 and later.Type: Bson Default: None |
以下代码展示了如何通过将comment()
方法链接到find_one_and_replace()
方法来设立comment
字段:
let res = my_coll.find_one_and_replace(filter, replacement) .comment(bson!("hello")) .await?;
查找和替换示例
此示例将执行以下动作:
调用
find_one_and_replace()
方法将查询过滤传递给
find_one_and_replace()
,以匹配name
的值包含string"Johnson"
的文档将描述新学生的替换文档传递给
find_one_and_replace()
将
return_document()
方法链接到find_one_and_replace()
以返回替换后的文档将
projection()
方法链接到输出中的find_one_and_replace()``to project only the ``name
和school
字段
let filter = doc! { "name": doc! { "$regex": "Johnson" } }; let replacement = doc! { "name": "Toby Fletcher", "age": 14, "school": "Durango High School" }; let res = my_coll.find_one_and_replace(filter, replacement) .return_document(ReturnDocument::After) .projection(doc! { "name": 1, "school": 1, "_id": 0 }) .await?; println!("Document after replacement:\n{:?}", res);
Document after replacement: Some(Document({"name": String("Toby Fletcher"), "school": String("Durango High School")}))
更多信息
要了解有关本指南中操作的更多信息,请参阅以下文档:
API 文档
要进一步了解本指南所提及的方法和类型,请参阅以下 API 文档: