文档菜单
文档首页
/ / /
Java 同步驱动程序
/ / /

在文档中更新数组

本页概述

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

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

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

  • 指定要执行的操作更新

  • 指定要将更新应用于哪些数组元素

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

以下部分包含更新此示例文档的示例

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

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

要指定更新,请使用 Updates 构造函数。 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 对象的属性访问语法。

有关更多信息,请参阅服务器手册条目点表示法.

要更新第一个与您的查询过滤器匹配的数组元素,请使用位置 $ 运算符。要使用位置 $ 运算符,数组字段必须作为您查询过滤器的一部分。

以下示例执行以下操作

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

  • 将匹配查询筛选条件的文档中的第一个数组值减少 "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] }

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

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

<标识符>是您为数组过滤器提供的名称。此值必须以小写字母开头,并且只能包含字母数字字符。

以下示例执行以下操作

  • 查询示例文档

  • 设置数组过滤器以搜索小于 "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] }

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

返回

修改