“文档” 菜单
文档首页
/ / /
Java (Sync) 驱动程序
/ / /

更新文档中的数组

在此页面上

  • 概述
  • 文档样本
  • 指定更新
  • 指定数组元素
  • 第一个匹配的数组元素
  • 匹配所有数组元素
  • 匹配多个数组元素

在本指南中,您可以了解如何使用 MongoDB Java 驱动程序更新文档中的数组。

要更新数组,必须执行以下操作:

  • 指定要执行的更新

  • 指定要应用更新的数组元素

  • 使用这些规范执行更新操作

以下各节提供更新此样本文档的示例:

{ "_id": 1, "color": "green", "qty": [8, 12, 18] }

本页上的示例使用MongoCollection类的 findOneAndUpdate()方法检索和更新文档。每个示例都使用FindOneAndUpdateOptions类的一个实例,让 MongoDB 在发生更新后检索文档。有关findOneAndUpdate()方法的更多信息,请参阅我们的复合操作指南。

要指定更新,请使用Updates构建器。 Updates构建器提供静态实用程序方法来构造更新规范。 有关将Updates构建器与数组结合使用的更多信息,请参阅我们的更新构建器指南。

下列示例执行以下操作:

  • 查询样本文档

  • 将“17”附加到文档中与查询筛选器匹配的 qty 数组中

Bson filter = Filters.eq("_id", 1);
Bson update = Updates.push("qty", 17);
// Defines options that configure the operation to return a document in its post-operation state
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER);
// Updates the first document that matches the filter and prints the updated document as JSON
Document result = collection.findOneAndUpdate(filter, update, options);
System.out.println(result.toJson());

前面的示例将原始文档更新为以下状态:

{ "_id": 1, "color": "green", "qty": [8, 12, 18, 17] }

可以使用位置运算符指定要更新的数组元素。位置运算符可以指定要更新的第一个、某几个或所有数组元素。

要使用位置运算符指定数组中的元素,请使用点符号。点符号是一种用于导航 BSON 对象的属性访问语法。

有关更多信息,请参阅有关MongoDB Server 点表示法 的 手册条目。

要更新与查询筛选器匹配的第一个数组元素,请使用位置$运算符。数组字段必须显示为查询筛选器的一部分才能使用位置$运算符。

下列示例执行以下操作:

  • 查询 qty 字段包含值“18”的文档

  • 将文档中与查询筛选器匹配的第一个数组值递减“3”

Bson filter = Filters.eq("qty", 18);
Bson update = Updates.inc("qty.$", -3);
// Defines options that configure the operation to return a document in its post-operation state
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER);
// Updates the first document that matches the filter and prints the updated document as JSON
Document result = collection.findOneAndUpdate(filter, update, options);
System.out.println(result.toJson());

前面的示例将原始文档更新为以下状态:

{ "_id": 1, "color": "green", "qty": [8, 12, 15] }

有关本节提及的方法和运算符的更多信息,请参阅以下资源:

要更新数组中的所有元素,请使用全位置运算符 $[]

下列示例执行以下操作:

  • 查询样本文档

  • 将与查询筛选器匹配的数组元素乘以“2”

Bson filter = Filters.eq("_id", 1);
Bson update = Updates.mul("qty.$[]", 2);
// Defines options that configure the operation to return a document in its post-operation state
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER);
// Updates the first document that matches the filter and prints the updated document as JSON
Document result = collection.findOneAndUpdate(filter, update, options);
System.out.println(result.toJson());

前面的示例将原始文档更新为以下状态:

{ "_id": 1, "color": "green", "qty": [16, 24, 36] }

有关本节提及的方法和运算符的更多信息,请参阅以下资源:

要更新与筛选器匹配的数组元素,请使用筛选的位置运算符 $[<identifier>]。您必须在更新操作中包含数组筛选器,以指定要更新的数组元素。

<identifier> 是您为数组筛选器指定的名称。此值必须以小写字母开头,并且只能包含字母数字字符。

下列示例执行以下操作:

  • 查询样本文档

  • 设置数组筛选器以搜索小于“15”的值

  • 将与查询筛选器匹配的数组元素递增“5”

Bson filter = Filters.eq("_id", 1);
Bson smallerFilter = Filters.lt("smaller", 15);
// Defines options that configure the document's return state and apply the array value filter
FindOneAndUpdateOptions options = new FindOneAndUpdateOptions()
.returnDocument(ReturnDocument.AFTER)
.arrayFilters(Arrays.asList(smallerFilter));
// Creates an update document to increase the matched array values by "5"
Bson update = Updates.inc("qty.$[smaller]", 5);
// Updates the first document that matches the filter and prints the updated document as JSON
Document result = collection.findOneAndUpdate(filter, update, options);
System.out.println(result.toJson());

前面的示例将原始文档更新为以下状态:

{ "_id": 1, "color": "green", "qty": [13, 17, 18] }

有关本节提及的方法和运算符的更多信息,请参阅以下资源:

后退

修改文档

来年

在单个操作中插入或更新