Docs 菜单
Docs 主页
/ /

Update Documents

在本指南中,您可以学习如何使用 updateOne()updateMany() 方法更新MongoDB集合中的文档。

本指南中的示例对 paint_inventory集合运行操作,该集合存储表示商店库存中油漆颜色的文档。此集合包含以下示例文档:

{ "_id": 1, "color": "red", "qty": 5, "prices": [15.99, 19.99] }
{ "_id": 2, "color": "purple", "qty": 8, "prices": [18.99, 22.99] }
{ "_id": 3, "color": "yellow", "qty": 0, "prices": [14.99, 17.99] }
{ "_id": 4, "color": "green", "qty": 6, "prices": [19.99, 24.99] }
{ "_id": 5, "color": "blue", "qty": 3, "prices": [17.99, 21.99] }

以下Kotlin数据类对该集合的文档进行建模:

data class PaintOrder(
@BsonId val id: Int,
val color: String,
val qty: Int,
val prices: List<Double>
)

您可以使用以下方法更新MongoDB中的文档:

  • updateOne(),更新匹配搜索条件的第一个文档

  • updateMany(),更新与搜索条件匹配的所有文档

每种更新方法都需要以下参数:

  • 查询过滤,用于匹配要更新的文档。 要学习;了解有关查询筛选器的更多信息,请参阅《 指定查询》指南。

  • 更新文档,指定更新操作符或要执行的更新类型,以及要更新的字段和值。有关更新操作符及其用法的列表,请参阅MongoDB服务器手册中的 字段更新操作符

本指南中的示例使用 Updates 构建器,这是一个提供构造更新文档的辅助方法的工厂类。要学习有关 Updates 构建者的更多信息,请参阅 更新构建者 指南。

此示例使用updateOne()方法执行以下操作:

  • 匹配 color字段值为 "yellow" 的文档

  • 使用 Updates 构建器将匹配文档的 qty字段值递增 1

val filter = Filters.eq(PaintOrder::color.name, "yellow")
val update = Updates.inc(PaintOrder::qty.name, 1)
val result = collection.updateOne(filter, update)
println("Matched document count: ${result.matchedCount}")
println("Modified document count: ${result.modifiedCount}")
Matched document count: 1
Modified document count: 1

注意

如果多个文档与传递给 updateOne() 方法的查询筛选条件匹配,则该操作仅更新第一个结果。要控制操作匹配哪个文档,可以指定排序顺序。要学习;了解有关 sort 选项的更多信息,请参阅排序结果页面。

以下示例使用updateMany() 方法执行以下操作:

  • 使用空查询筛选条件匹配所有文档

  • 使用 Updates 构建器将所有匹配文档中的 qty字段值增加 20

val filterMany = Filters.empty()
val updateMany = Updates.inc(PaintOrder::qty.name, 20)
val resultMany = collection.updateMany(filterMany, updateMany)
println("Matched document count: ${resultMany.matchedCount}")
println("Modified document count: ${resultMany.modifiedCount}")
Matched document count: 5
Modified document count: 5

更新操作后,paint_inventory集合中的文档具有以下值:

{ "_id": 1, "color": "red", "qty": 25, "prices": [15.99, 19.99] }
{ "_id": 2, "color": "purple", "qty": 28, "prices": [18.99, 22.99] }
{ "_id": 3, "color": "yellow", "qty": 20, "prices": [14.99, 17.99] }
{ "_id": 4, "color": "green", "qty": 26, "prices": [19.99, 24.99] }
{ "_id": 5, "color": "blue", "qty": 23, "prices": [17.99, 21.99] }

注意

如果查询筛选条件与任何文档均不匹配,则 updateMany() 方法不会对集合中的文档进行更改。如果没有匹配的文档,可以使用 upsert 选项插入新文档。要查看使用此选项的示例,请参阅在单个操作中插入或更新。

重要

updateOne()updateMany() 方法无法对违反集合唯一索引约束的文档进行更改。要学习;了解更多信息,请参阅MongoDB服务器手册中的唯一索引

要更新文档的数组值,请使用 Updates 构建器指定要执行的更新以及要更新的数组元素。然后,调用 updateOne()updateMany() 方法运行更新操作。

您可以使用以下位置运算符指定要更新的数组元素:

  • $:更新与查询筛选条件匹配的第一个数组元素。

  • $[]:更新数组中的所有元素。

  • $[<identifier>]:更新与过滤器匹配的数组元素。

本节中的示例展示了如何使用每个位置操作符。

要更新与查询筛选条件匹配的第一个数组元素,请使用位置 ($)操作符并指定在查询筛选条件中要更新的数组字段。

此示例将执行以下动作:

  • 匹配具有包含 15.99 值的 prices大量的文档

  • 使用 $ 位置操作符将匹配文档的 prices数组中的第一个值增加 2

val filterArrayFirst = Filters.eq(PaintOrder::prices.name, 15.99)
val updateArrayFirst = Updates.inc("${PaintOrder::prices.name}.$", 2)
val resultArrayFirst = collection.updateOne(filterArrayFirst, updateArrayFirst)
println("Modified document count: ${resultArrayFirst.modifiedCount}")
Modified document count: 1

要更新数组中的所有元素,请使用 all 位置操作符($[])。

此示例将执行以下动作:

  • 匹配 color 值为 "green" 的文档

  • 使用 $[] 位置操作符将匹配文档的 prices数组中的所有值乘以 1.1

后退

查询文本

在此页面上