Docs 菜单
Docs 主页
/ / /
Java (Sync) 驱动程序
/ / /

修改文档

在此页面上

  • 概述
  • 更新
  • 更新操作参数
  • 替换
  • 替换操作参数

在本指南中,您可以了解如何使用两种不同的操作类型修改 MongoDB 集合中的文档:

  • 更新

  • 替换

更新操作指定一个或多个文档中要更改的字段和值。替换操作指定用于替换collection中单个文档的字段和值。

在以下示例中,一家油漆店销售五种不同颜色的油漆。 paint_inventory collection 表示其当前库存:

{ "_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()不会对集合中的文档进行任何更改。 请参阅我们的更新或插入指南,了解如何在没有匹配的文档时插入新文档而不是更新文档。

重要

updateOne()updateMany() 方法无法更改违反集合上唯一索引约束的文档。有关唯一索引约束的更多信息,请参阅 MongoDB Server 手册中的唯一索引

替换操作会替换集合中的一个文档。 替换发生在查询筛选器匹配的文档和替换文档之间。

replaceOne() 方法会删除匹配文档中的所有现有字段和值(_id 字段除外),并用替换文档进行替换。

您可以在MongoCollection实例上调用replaceOne()方法,如下所示:

collection.replaceOne(query, replacementDocument);

replaceOne() 方法具有以下参数:

  • query 指定带有条件的查询筛选器,以匹配集合中要替换的文档

  • replacementDocument 指定要在匹配文档中替换的新Document对象的字段和值

油漆店意识到他们必须再次更新库存。 他们以为是 20 罐粉红色油漆,实际上是 25 罐橙色油漆。

要更新清单,请调用replaceOne()方法并指定以下内容:

  • 查询筛选器,匹配color为“粉红色”的文档

  • 替换文档,其中color为“orange”, qty为“ 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()不会对集合中的文档进行任何更改。 请参阅我们的更新或插入指南,了解如何在没有匹配的文档时插入新文档而不是替换文档。

如果多个文档与 replaceOne() 方法中指定的查询过滤器匹配,则替换第一个结果。

重要

replaceOne()方法无法对违反集合唯一索引约束的文档进行更改。 有关唯一索引约束的更多信息,请参阅 MongoDB Server 手册中的唯一索引

后退

删除文档

来年

更新文档中的数组