修改文档
概述
在本指南中,您可以了解如何使用两种不同的操作类型修改MongoDB集合中的文档
更新操作指定了要更改一个或多个文档中的字段和值。替换操作指定了替换集合中单个文档的字段和值。
在以下示例中,一家油漆店销售五种不同颜色的油漆。以下paint_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 }
该数据使用以下Kotlin数据类进行建模
data class PaintOrder( val id: Int, val color: String, val qty: Int )
更新
更新操作可以修改字段和值。它们将更新文档中指定的更改应用于一个或多个与您的查询过滤器匹配的文档。
方法 updateOne() 修改查询过滤器匹配的第一个文档,而方法 updateMany() 则修改查询过滤器匹配的所有文档。
您可以在 MongoCollection
实例上调用 updateOne()
和 updateMany()
方法,如下所示
collection.updateOne(query, updateDocument) collection.updateMany(query, updateDocument)
更新操作参数
updateOne()
和 updateMany()
方法都具有以下参数
query
指定一个查询过滤器,以匹配要更新的文档updateDocument
指定要修改匹配文档的字段和值。在此示例中,我们使用Updates builder 来创建更新文档。
您可以使用 Updates
构造器来创建 updateDocument
,如下所示
val updateDocument = Updates.operator(field, value)
请参阅MongoDB API文档,了解完整的更新构建器列表及其用法。点击此处查看
示例
油漆店需要在顾客退还一罐黄色油漆后更新其库存。
要更新单罐油漆,请调用updateOne()
方法,指定以下内容
一个查询过滤器,匹配黄色颜色
一个更新文档,包含将
qty
字段增加"1"的指令
val filter = Filters.eq(PaintOrder::color.name, "yellow") val update = Updates.inc(PaintOrder::qty.name, 1) val result = collection.updateOne(filter, update) println("Matched document count: $result.matchedCount") println("Modified document count: $result.modifiedCount")
Matched document count: 1 Modified document count: 1
油漆店随后收到一批新货,需要再次更新库存。该批次包含20罐每种颜色的油漆。
要更新库存,请调用updateMany()
方法,指定以下内容
一个查询过滤器,匹配所有颜色
一个更新文档,包含将
qty
字段增加"20"的指令
val filter = Filters.empty() val update = Updates.inc(PaintOrder::qty.name, 20) val result = collection.updateMany(filter, update) println("Matched document count: $result.matchedCount") println("Modified document count: $result.modifiedCount")
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”
val filter = Filters.eq(PaintOrder::color.name, "pink") val update = PaintOrder(5, "orange", 25) val result = collection.replaceOne(filter, update) println("Matched document count: $result.matchedCount") println("Modified document count: $result.modifiedCount")
Matched document count: 1 Modified document count: 1
以下显示了更新的文档
{ "_id": 5, "color": "orange", "qty": 25 }
如果在替换操作中查询过滤器没有匹配到任何文档,则replaceOne()
不会对集合中的文档进行任何更改。有关在没有匹配到文档时插入新文档而不是替换现有文档的说明,请参阅我们的upsert指南。
如果多个文档匹配replaceOne()
方法中指定的查询过滤器,则它将替换第一个结果。
重要
replaceOne()
方法无法更改违反集合上唯一索引约束的文档。有关更多信息,请参阅MongoDB服务器手册中的唯一索引。