Docs 菜单
Docs 主页
/ / /
java sync
/ /

更新构建器

在此页面上

  • 概述
  • 字段更新
  • 未设置
  • Set On Insert
  • 增量
  • rename
  • Max
  • 当前日期
  • 当前时间戳
  • Bit
  • 数组更新
  • Add to Set
  • Pop
  • Pull All
  • Pull
  • 推动
  • 组合多个更新运算符

在本指南中,您可以了解如何在 MongoDB Java 驱动程序中使用 构建器 指定 更新 。

Updates 构建器为以下类型的更新提供辅助方法:

  • 字段更新

  • 数组更新

  • 组合多个更新运算符

一些需要更新的方法包括:

  • updateOne()

  • updateMany()

  • bulkWrite()

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

提示

为简洁起见,你可以选择静态导入更新类的方法:

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

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

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

{
"_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);
collection.updateOne(filter, update);

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

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

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

以下示例删除了 qty 字段:

Bson filter = eq("_id", 1);
Bson update = unset("qty");
collection.updateOne(filter, update);

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

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

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

如果更新或插入导致插入文档,以下示例将 qty 字段的值设置为“5”:

Bson filter = eq("_id", 1);
Bson update = setOnInsert("qty", 7);
collection.updateOne(filter, update, new UpdateOptions().upsert(true));

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

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

注意

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

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

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

Bson filter = eq("_id", 1);
Bson update = inc("qty", 3);
collection.updateOne(filter, update);

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

{
"_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);
collection.updateOne(filter, update);

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

{
"_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");
collection.updateOne(filter, update);

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

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

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

Bson filter = eq("_id", 1);
Bson update = min("qty", 2);
collection.updateOne(filter, update);

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

{
"_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);
collection.updateOne(filter, update);

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

{
"_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");
collection.updateOne(filter, update);

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

{
"_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");
collection.updateOne(filter, update);

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

{
"_id": 1,
"color": "red",
"qty": 5,
"vendor": [ "A", "D", "M" ],
"lastModified": { "$timestamp": { "t": 1616446880, "i": 5 } }
}

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

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

Bson filter = eq("_id", 1);
Bson update = bitwiseOr("qty", 10);
collection.updateOne(filter, update);

按位运算的结果为 15:

0101
1010
----
1111

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

{
"_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");
collection.updateOne(filter, update);

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

{
"_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");
collection.updateOne(filter, update);

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

{
"_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"));
collection.updateOne(filter, update);

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

{
"_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");
collection.updateOne(filter, update);

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

{
"_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");
collection.updateOne(filter, update);

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

{
"_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"));
collection.updateOne(filter, update);

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

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

后退

排序构建器