Docs 菜单
Docs 主页
/ / /
Java Reactive Streams 驱动程序
/

替换文档

在此页面上

  • Overview
  • 样本数据
  • 替换操作
  • 所需参数
  • 替换示例
  • 修改替换行为
  • 修改替换示例
  • 返回值
  • 更多信息
  • API 文档

在本指南中,您可以了解如何使用 Java Reactive Streams 驱动程序通过执行替换 操作来替换 MongoDB 集合中的文档。

替换操作使用您指定的新字段和值替换 MongoDB 集合中的一个文档。您可以使用 replaceOne()方法执行替换操作。

本指南中的示例使用 Atlas 示例数据集 中的 sample_restaurants.restaurants 集合。

要了解如何创建免费的 MongoDB Atlas 集群并加载样本数据集,请参阅入门教程。

重要

项目 Reactor 库

本指南使用 Project Reactor 库来使用Publisher Java Reactive Streams 驱动程序方法返回的 实例。要了解有关 Project Reactor 库及其使用方法的更多信息,请参阅 入门 在 Reactor 文档中。要进一步了解如何使用本指南中的 Project Reactor 库方法,请参阅“ 将数据写入 MongoDB ”指南。

您可以在MongoCollection实例上使用replaceOne()方法执行替换操作。此方法会从匹配搜索条件的第一个文档中删除除_id字段之外的所有字段,然后将您指定的字段和值添加到空文档中。

replaceOne() 方法需要使用以下参数:

  • 查询筛选器文档,用于确定要替换的文档。

有关查询筛选器的更多信息,请参阅“指定查询”指南。

  • 替换文档,指定要插入新文档中的字段和值。

要替换 MongoDB 集合中的单个文档,请调用replaceOne() 方法并将查询筛选器文档和替换文档作为参数传递。然后,将replaceOne() 结果传递给Mono.from() 中的静态Mono Mono方法。 是 Project Reactor 库中的一个类。在 Java Reactive Streams 中,驱动程序方法会返回Publisher 冷实例,这意味着除非您订阅返回的Publisher ,否则不会发生相应的操作。本指南使用 Project Reactor 库来使用它们。要了解有关Mono 的更多信息,请参阅 Mono 在 Project Reactor 文档中。

以下示例使用replaceOne()方法将name字段值为"Pizza Town"的文档的字段和值替换。 replaceOne()方法将原始文档替换为name字段值为"Mongo's Pizza"cuisine字段值为"Pizza"的文档。

Publisher<UpdateResult> replacePublisher = restaurants.replaceOne(
eq("name", "Pizza Town"),
new Document().append("name", "Mongo's Pizza")
.append("cuisine", "Pizza"));
Mono.from(replacePublisher).block();

您可以选择通过调用选项方法来修改replaceOne()方法的行为。 ReplaceOptions类提供了修改replaceOne()方法行为的方法。要使用ReplaceOptions类,请构造该类的新实例,然后调用其一个或多个方法来修改替换操作。您可以将这些方法调用链接在一起。要修改替换操作的行为,请将类实例和链式方法调用作为最后一个参数传递给replaceOne()方法。

您可以使用ReplaceOptions类中的以下方法来修改replaceOne()方法。

方法
说明
bypassDocumentValidation(Boolean bypass)
Specifies whether the replace operation bypasses document validation. This lets you update documents that don't meet the schema validation requirements, if any exist. For more information about schema validation, see Schema Validation in the MongoDB Server manual.
collation(Collation collation)
Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.
comment(BsonValue comment)
Attaches a BsonValue comment to the operation. For more information, see the insert command fields guide in the MongoDB Server manual.
comment(String comment)
Attaches a String comment to the operation. For more information, see the insert command fields guide in the MongoDB Server manual.
hint(Bson hint)
Sets the index for the operation as a Bson value. For more information, see the hint statement in the MongoDB Server manual.
hintString(String hint)
Sets the index for the operation as a String value. For more information, see the hint statement in the MongoDB Server manual.
let(Bson variables)
Specifies a map of parameter names and values. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual.
upsert(Boolean upsert)
Specifies whether the replace operation performs an upsert operation if no documents match the query filter. For more information, see the upsert statement in the MongoDB Server manual.

以下代码使用replaceOne()方法替换restaurants集合中的文档。它还设置upsert(true)选项,以便驱动程序在查询筛选器与任何现有文档都不匹配时插入新文档。

Publisher<UpdateResult> replacePublisher = restaurants.replaceOne(
eq("name", "Food Town"),
new Document().append("name", "Food World")
.append("cuisine", "Mixed"),
new ReplaceOptions().upsert(true));
Mono.from(replacePublisher).block();

replaceOne()方法返回一个UpdateResult对象。使用UpdateResult类型中的以下方法访问相应信息:

属性
说明
getMatchedCount()
The number of documents that matched the query filter, regardless of how many were replaced.
getModifiedCount()
The number of documents modified by the replace operation. If a replaced document is identical to the original, it is not included in this count.
getUpsertedId()
The ID of the document that was inserted into the database, if the driver performed an upsert. If no document was inserted, this value is null.
wasAcknowledged()
An acknowledgement of the replacement.

有关使用 Java Reactive Streams 驱动程序替换文档的可运行代码示例,请参阅Write Data to MongoDB指南。

要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档:

后退

Update Documents