修改文档
概述
在本指南中,您可以学习如何使用两种不同的操作类型修改 MongoDB 集合中的文档
更新操作指定要在一个或多个文档中更改的字段和值。替换操作指定要替换集合中单个文档的字段和值。
在以下示例中,一家油漆店销售五种不同颜色的油漆。Thepaint_inventory
集合代表他们的当前库存
{ "_id": 1, "color": "red", "qty": 5 } { "_id": 2, "color": "purple", "qty": 8 } { "_id": 3, "color": "yellow", "qty": 0 } { "_id": 4, "color": "green", "qty": 6 } { "_id": 5, "color": "pink", "qty": 0 }
更新
更新操作可以修改字段和值。它们将更新文档中指定的更改应用于匹配您的查询筛选条件的单个或多个文档。
方法 updateOne() 更改与您的查询过滤器匹配的第一个文档,而 updateMany() 方法则更改所有与您的查询过滤器匹配的文档。
您可以在 MongoCollection
实例上调用 updateOne()
和 updateMany()
方法,如下所示:
collection.updateOne(query, updateDocument); collection.updateMany(query, updateDocument);
更新操作参数
方法 updateOne()
和 updateMany()
都有以下参数:
query
指定一个查询过滤器,用于匹配要更新的文档中的标准updateDocument
指定要修改匹配文档或文档的哪些字段和值。在此示例中,我们使用更新构建器 创建更新文档。
您可以使用 Updates
构建器创建 updateDocument
,如下所示:
Bson updateDocument = Updates.operator(field, value);
请参阅MongoDB API文档以获取更新构建器和其使用的完整列表。(请参阅更新构建器和其使用的完整列表。)
示例
油漆店收到一批新货,需要更新库存。这批货包含每种油漆颜色的20罐。
要更新库存,请调用updateMany()
方法,指定以下内容
一个查询过滤器,匹配所有颜色
一个更新文档,包含将
qty
字段增加“20”的指令
Bson filter = Filters.empty(); Bson update = Updates.inc("qty", 20); // Updates all documents and prints the number of matched and modified documents UpdateResult result = collection.updateMany(filter, update); System.out.println("Matched document count: " + result.getMatchedCount()); System.out.println("Modified document count: " + result.getModifiedCount());
上述代码的输出类似于以下内容
Matched document count: 5 Modified document count: 5
以下展示了更新后的paint_inventory
集合中的文档
{ "_id": 1, "color": "red", "qty": 25 } { "_id": 2, "color": "purple", "qty": 28 } { "_id": 3, "color": "yellow", "qty": 20 } { "_id": 4, "color": "green", "qty": 26 } { "_id": 5, "color": "pink", "qty": 20 }
如果更新操作中的查询过滤器没有匹配到任何文档,则updateMany()
不会对集合中的文档进行任何更改。有关在无匹配文档时插入新文档而不是更新文档的说明,请参阅我们的upsert指南。
重要
updateOne()
和updateMany()
方法不能对违反集合唯一索引约束的文档进行更改。有关唯一索引约束的更多信息,请参阅MongoDB服务器手册中的唯一索引。
替换
替换操作会用你的集合中的一个文档替换另一个文档。替换发生在查询过滤器匹配的文档和替换文档之间。
replaceOne() 方法会删除匹配文档中所有现有的字段和值(除了 _id
字段),并用替换文档替换它。
你可以在 MongoCollection
实例上调用 replaceOne()
方法,如下所示
collection.replaceOne(query, replacementDocument);
替换操作参数
replaceOne()
方法有以下参数
query
指定一个查询过滤器,用于匹配要替换的文档replacementDocument
指定替换匹配文档的新Document
对象的字段和值
示例
油漆店意识到他们必须再次更新库存。他们原本以为有20罐粉色油漆,实际上有25罐橙色油漆。
要更新库存,请调用replaceOne()
方法,指定以下内容
一个查询过滤器,匹配颜色为“粉色”的文档
一个替换文档,颜色为“橙色”,数量为“25”
Bson filter = Filters.eq("color", "pink"); Document document = new Document("color", "orange").append("qty", 25); // Replaces the first document that matches the filter with a new document UpdateResult result = collection.replaceOne(filter, document); // Prints the number of matched and modified documents System.out.println("Matched document count: " + result.getMatchedCount()); System.out.println("Modified document count: " + result.getModifiedCount());
上述代码的输出类似于以下内容
Matched document count: 1 Modified document count: 1
以下显示更新后的文档
{ "_id": 5, "color": "orange", "qty": 25 }
如果在替换操作中查询过滤器没有匹配到任何文档,则replaceOne()
不会对集合中的文档进行任何更改。有关如何在没有匹配到文档时插入新文档而不是替换文档的说明,请参阅我们的upsert指南。
如果在replaceOne()
方法中指定的查询过滤器匹配到多个文档,则它将替换第一个结果。
重要
replaceOne()
方法不能更改违反集合上唯一索引约束的文档。有关唯一索引约束的更多信息,请参阅MongoDB服务器手册中的唯一索引。