对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

更新构建器

在本指南中,您可以学习;了解如何使用Java Reactive Streams驾驶员中的构建者来指定更新。

Updates构建器提供辅助方法来简化以下任务:

需要更新文档的一些方法是:

  • updateOne()

  • updateMany()

  • bulkWrite()

Updates类为所有MongoDB更新操作符提供静态工厂方法。每个方法都返回一个BSON类型的实例,您可以将其传递给任何需要更新参数的方法。

提示

为简洁起见,您可以选择静态导入 Updates Filters 类的方法:

import static com.mongodb.client.model.Updates.*;
import static com.mongodb.client.model.Filters.*;

以下示例假定此静态导入。

本指南中的示例使用以下文档:

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

使用 set() 方法在更新操作中分配字段的值。

以下示例将 qty 字段的值设置为 "11":

Bson filter = eq("_id", 1);
Bson update = set("qty", 11);
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 11,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

以下示例将向原始文档添加两个新字段:

Bson filter = eq("_id", 1);
Bson update = combine(set("width", 6.5), set("height", 10));
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" },
"width": 6.5,
"height": 10
}

使用 unset() 方法在更新操作中删除字段的值。

以下示例删除了 qty 字段:

Bson filter = eq("_id", 1);
Bson update = unset("qty");
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

使用 setOnInsert() 方法在插入文档的更新操作中分配字段的值。

如果更新或插入(upsert)导致插入文档,以下示例会将 qty字段的值设置为 "7":

Bson filter = eq("_id", 1);
Bson update = setOnInsert("qty", 7);
Publisher<UpdateResult> result = collection.updateOne(filter, update, new UpdateOptions().upsert(true));
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 7,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

注意

如果未插入文档,则不会发生任何更改。

使用 inc() 方法在更新操作中增加数字字段的值。

以下示例将 qty 的值递增“3”:

Bson filter = eq("_id", 1);
Bson update = inc("qty", 3);
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 8,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

使用 mul() 方法在更新操作中倍增数字字段的值。

以下示例将 qty 的值乘以 "2":

Bson filter = eq("_id", 1);
Bson update = mul("qty", 2);
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 10,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

使用 rename() 方法在更新操作中重命名字段的值。

以下示例将 qty 字段重命名为 "quantity":

Bson filter = eq("_id", 1);
Bson update = rename("qty", "quantity");
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" },
"quantity": 5
}

使用 min() 方法,利用更新操作中两个指定字段中较小的数字来更新字段的值。

以下示例将 qty字段的值设置为其当前值和“2”中的最小值:

Bson filter = eq("_id", 1);
Bson update = min("qty", 2);
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 2,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

使用 max() 方法,利用更新操作中两个指定字段中较大的数字来更新字段的值。

以下示例将 qty 字段的值设置为其当前值和数字 “8” 中的较大者:

Bson filter = eq("_id", 1);
Bson update = max("qty", 8);
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 8,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

使用currentDate()方法将更新操作中的字段值赋给当前日期以作为BSON日期。

以下示例将 lastModified 字段的值设置为当前日期(作为 BSON 日期):

Bson filter = eq("_id", 1);
Bson update = currentDate("lastModified");
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-22T21:01:20.027Z" }
}

使用currentTimestamp()方法将更新操作中的字段值指定为当前日期以作为时间戳。

以下示例将 lastModified 字段的值设置为当前日期(作为 BSON 时间戳 ):

Bson filter = eq("_id", 1);
Bson update = currentTimestamp("lastModified");
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$timestamp": { "t": 1616446880, "i": 5 } }
}

使用 bitwiseOr()bitwiseAnd()bitwiseXor() 方法,在更新操作中对字段的整数值执行按位更新。

以下示例在数字 "10" 和 qty 字段的整数值之间执行按位 OR

Bson filter = eq("_id", 1);
Bson update = bitwiseOr("qty", 10);
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();

按位运算的结果为 15:

0101 // 5 in binary
1010 // 10 in binary
----
1111 // 15 in binary

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

{
"_id": 1,
"color": "red",
"qty": 15,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

如果更新操作中尚不存在某值,则使用 addToSet() 方法将该值附加到数组中。

以下示例将“C”值添加到 vendor 字段的数组值中:

Bson filter = eq("_id", 1);
Bson update = addToSet("vendor", "C");
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M", "C" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

使用 popFirst() 方法删除数组的第一个元素;使用 popLast() 方法在更新操作中删除数组的最后一个元素。

以下示例会从 vendor 字段的数组值中弹出第一个元素:

Bson filter = eq("_id", 1);
Bson update = popFirst("vendor");
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "D", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

使用 pullAll() 方法在更新操作中从现有数组中删除值的所有实例。

以下示例从 vendor 阵列中删除了供应商 "A" 和 "M":

Bson filter = eq("_id", 1);
Bson update = pullAll("vendor", Arrays.asList("A", "M"));
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "D" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

使用 pull() 方法在更新操作中从现有数组中删除值的所有实例。

以下示例从 vendor 数组中删除值“D”:

Bson filter = eq("_id", 1);
Bson update = pull("vendor", "D");
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "M" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

使用 push() 方法在更新操作中将值附加到数组。

以下示例将“C”推送到 vendor 数组:

Bson filter = eq("_id", 1);
Bson update = push("vendor", "C");
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M", "C" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}

应用程序可以通过组合前面章节所述的两个或多个更新运算符来更新单个文档的多个字段。

以下示例将 qty 字段的值增加“6”,将 color 字段的值设置为“purple”,并将“R”推送到 vendor 字段:

Bson filter = eq("_id", 1);
Bson update = combine(set("color", "purple"), inc("qty", 6), push("vendor", "R"));
Publisher<UpdateResult> result = collection.updateOne(filter, update);
Mono.from(result).block();
{
"_id": 1,
"color": "purple",
"qty": 11,
"vendor": [ "A", "D", "M", "R" ],
"lastModified": { "$date": "2021-03-05T05:00:00Z" }
}