更新构建器
概述
在本指南中,您可以了解如何使用以下方式指定更新:Java 驱动中的构建器。
关于Updates
构建器提供了以下任务的辅助方法以简化操作
一些预期需要更新文档的方法有
updateOne()
updateMany()
bulkWrite()
Updates
类为所有 MongoDB 更新操作符提供了静态工厂方法。每个方法返回一个 BSON 类型的实例,您可以将它传递给任何需要更新参数的方法。
提示
本指南中的示例使用以下文档
{ "_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" } }
以下示例向原始文档添加两个新字段
Bson filter = eq("_id", 1); Bson update = combine(set("width", 6.5), set("height", 10)); collection.updateOne(filter, update);
前面的示例将原始文档更新为以下状态
{ "_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"); collection.updateOne(filter, update);
前面的示例将原始文档更新为以下状态
{ "_id": 1, "color": "red", "vendor": [ "A", "D", "M" ], "lastModified": { "$date": "2021-03-05T05:00:00Z" } }
插入时设置
使用 setOnInsert() 方法在插入文档时在更新操作中为字段赋值。
以下示例在upsert操作导致文档插入时将 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()方法在更新操作中增加数值字段的值。
以下示例通过“3”增加了qty
字段的值。
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" } }
Pop
使用 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()方法,从更新操作中的现有数组中移除所有值的实例。
以下示例从供应商
数组中移除供应商"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" } }