在MongoDB中更新文档的4种方法
MongoDB提供了多种方法来更新文档。您使用的方法将取决于您希望如何执行更新。
本文介绍了在MongoDB中更新文档的4种方法。
方法db.collection.updateOne()
该方法完全符合其名称的承诺 - 它更新一个文档。db.collection.updateOne()
假设我们有一个名为 pets 的集合,其中包含以下文档:
{ “_id” : 1, “name” : “Wag”, “type” : “Dog” } { “_id” : 2, “name” : “Bark”, “type” : “Dog” } { “_id” : 3, “name” : “Meow”, “type” : “Cat” }
我们可以像这样更新一个文档:
db.pets.updateOne(
{ type: "Dog" },
{ $set: { type: "Cow" } }
)
结果:
{ “acknowledged” : true, “matchedCount” : 1, “modifiedCount” : 1 }
这只更新了一个文档,即使两个文档符合过滤条件(条件为 )。type: "Dog"
我们可以像这样检查结果:
db.pets.find()
结果:
{ “_id” : 1, “name” : “Wag”, “type” : “Cow” } { “_id” : 2, “name” : “Bark”, “type” : “Dog” } { “_id” : 3, “name” : “Meow”, “type” : “Cat” }
第一个文档现在具有 of 而不是 ,但第二个文档不受影响,即使它也符合筛选条件。type``Cow``Dog
方法db.collection.updateMany()
该方法更新与集合的指定筛选器匹配的所有文档。db.collection.updateMany()
让我们使用原始收集文档:
{ “_id” : 1, “name” : “Wag”, “type” : “Dog” } { “_id” : 2, “name” : “Bark”, “type” : “Dog” } { “_id” : 3, “name” : “Meow”, “type” : “Cat” }
再一次,我们可以看到两个文档具有作为它们的.Dog``type
我们可以像这样一次更新两个文档:
db.pets.updateMany(
{ type: "Dog" },
{ $set: { type: "Cow" } }
)
输出:
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }这向我们显示两个文档匹配,两个文档已更新。
我们可以检查集合:
db.pets.find()
输出:
{ "_id" : 1, "name" : "Wag", "type" : "Cow" }
{ "_id" : 2, "name" : "Bark", "type" : "Cow" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }方法db.collection.update()
The method can update a single document or multiple documents in a collection.db.collection.update()
By default, it only updates a single document. But if is specified, then it updates all documents that match the query criteria.multi: true
Update a Single Document
Let’s use the original collection of documents again:
{ “_id” : 1, “name” : “Wag”, “type” : “Dog” } { “_id” : 2, “name” : “Bark”, “type” : “Dog” } { “_id” : 3, “name” : “Meow”, “type” : “Cat” }
我们可以像这样更新一个文档:
db.pets.update(
{ type: "Dog" },
{ $set: { type: "Cow" } }
)
结果:
WriteResult({ “nMatched” : 1, “nUpserted” : 0, “nModified” : 1 })
只更新了一个文档。当我们查询集合时,会确认这一点。
db.pets.find()
结果:
{ “_id” : 1, “name” : “Wag”, “type” : “Cow” } { “_id” : 2, “name” : “Bark”, “type” : “Dog” } { “_id” : 3, “name” : “Meow”, “type” : “Cat” }
更新多个文档
让我们再次回到原始文档集合:
{ “_id” : 1, “name” : “Wag”, “type” : “Dog” } { “_id” : 2, “name” : “Bark”, “type” : “Dog” } { “_id” : 3, “name” : “Meow”, “type” : “Cat” }
现在,我们将添加到更新操作中,以更新与查询条件匹配的所有文档:multi: true
db.pets.update(
{ type: "Dog" },
{ $set: { type: "Cow" } },
{ multi: true }
)
结果:
WriteResult({ “nMatched” : 2, “nUpserted” : 0, “nModified” : 2 })
因此,这次匹配并更新了两个文档。
让我们再看看我们的收藏:
db.pets.find()
结果:
{ “_id” : 1, “name” : “Wag”, “type” : “Cow” } { “_id” : 2, “name” : “Bark”, “type” : “Cow” } { “_id” : 3, “name” : “Meow”, “type” : “Cat” }
正如预期的那样,这两个文档现在都有一个 .type``Cow
方法db.collection.replaceOne()
该方法基于筛选器替换集合中的单个文档。db.collection.replaceOne()
再次使用原始集合:
{ “_id” : 1, “name” : “Wag”, “type” : “Dog” } { “_id” : 2, “name” : “Bark”, “type” : “Dog” } { “_id” : 3, “name” : “Meow”, “type” : “Cat” }
让我们看看当我们使用该方法对付它时会发生什么。db.collection.replaceOne()
db.pets.replaceOne(
{ type: "Dog" },
{ type: "Cow" }
)
结果:
{ “acknowledged” : true, “matchedCount” : 1, “modifiedCount” : 1 }
更新了一份文件。
一起来看看吧。
db.pets.find()
结果:
{ “_id” : 1, “type” : “Cow” } { “_id” : 2, “name” : “Bark”, “type” : “Dog” } { “_id” : 3, “name” : “Meow”, “type” : “Cat” }
这一次,整个文档被新文档替换(字段除外)。_id
此方法替换整个文档(字段除外)。_id
更新插入
上述所有方法都接受使您能够执行更新插入操作的参数。upsert
当 时,如果与查询条件匹配,则会更新文档,但如果不匹配,则会插入一个新文档。upsert: true
例:
db.pets.updateOne(
{ name: "Wag" },
{ $set: { type: "Cow" } },
{ upsert: true }
)
结果:
{ “acknowledged” : true, “matchedCount” : 0, “modifiedCount” : 0, “upsertedId” : ObjectId(“5fe1d5aad991410169410165”) }
在这种情况下,没有匹配项,因此更新了文档。
让我们检查一下集合。
db.pets.find()
结果:
{ “_id” : 1, “type” : “Cow” } { “_id” : 2, “name” : “Bark”, “type” : “Dog” } { “_id” : 3, “name” : “Meow”, “type” : “Cat” } { “_id” : ObjectId(“5fe1d5aad991410169410165”), “name” : “Wag”, “type” : “Cow” }