MongoDBでは、db.collection.update() メソッドは、既存のドキュメントまたはコレクション内のドキュメントを変更します。
デフォルトでは、1つのドキュメントのみが更新されます。ただし、multi: trueの場合 を指定すると、クエリ条件に一致するすべてのドキュメントが更新されます。
単一のドキュメントを更新する
petsというコレクションがあるとします。 次のドキュメントを使用:
{ "_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 }) 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 }) そのため、今回は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.update() メソッドはupsertを受け入れます アップサート操作を実行できるようにする引数。
upsert: trueの場合 、フィルタ条件に一致する場合はドキュメントが更新されますが、一致しない場合は新しいドキュメントが挿入されます。
元のドキュメントからもう一度始めましょう:
{ "_id" : 1, "name" : "Wag", "type" : "Dog" }
{ "_id" : 2, "name" : "Bark", "type" : "Dog" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" } 例:
db.pets.update(
{ name: "Bubbles" },
{ $set: { type: "Fish" } },
{ upsert: true }
) 結果:
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("5fe2c925d9914101694102e1")
}) この場合、一致するものがなかったため、ドキュメントがアップサートされました。
コレクションを確認しましょう。
db.pets.find() 結果:
{ "_id" : 1, "name" : "Wag", "type" : "Dog" }
{ "_id" : 2, "name" : "Bark", "type" : "Dog" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }
{ "_id" : ObjectId("5fe2c925d9914101694102e1"), "name" : "Bubbles", "type" : "Fish" } 埋め込みドキュメント
db.collection.update()を使用することもできます 埋め込まれたドキュメントを更新します。
次のドキュメントがあるとします。
{
"_id" : 1,
"name" : "Wag",
"type" : "Dog",
"specs" : {
"height" : 400,
"weight" : 15,
"color" : "brown"
}
} 次のコードを使用して、埋め込まれたドキュメントを更新できます。
db.pets.update({
_id: 1
}, {
$set: {
"specs.weight": 20,
"specs.color": "blue"
}
}) 結果:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) したがって、1つのドキュメントが一致して変更されたことがわかります。
ドキュメントを確認しましょう。
db.pets.find({
_id: 1
}).pretty() 結果:
{
"_id" : 1,
"name" : "Wag",
"type" : "Dog",
"specs" : {
"height" : 400,
"weight" : 20,
"color" : "blue"
}
} 埋め込まれたドキュメントが指定どおりに更新されたことがわかります。
配列
db.collection.update()を使用しましょう アレイを更新します。
次のドキュメントがあるとします。
{
"_id" : 1,
"name" : "Wag",
"type" : "Dog",
"awards" : [
"Top Dog",
"Best Dog",
"Biggest Dog"
]
}> 2つの配列要素と犬の名前を更新しましょう。
db.pets.update({
_id: 1
}, {
$set: {
"name": "Bark",
"awards.0": "Bottom Dog",
"awards.1": "Worst Dog"
}
}) 結果:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) 1つのドキュメントが一致して変更されたことがわかります。
そして今、ドキュメントを見てください。
db.pets.find().pretty() 結果:
{
"_id" : 1,
"name" : "Bark",
"type" : "Dog",
"awards" : [
"Bottom Dog",
"Worst Dog",
"Biggest Dog"
]
}> arrayFiltersパラメーター
arrayFiltersを使用することもできます パラメータと位置の$ 更新する配列要素を決定する演算子。
たとえば、playersというコレクションがあるとします。 次のドキュメントを使用:
{ "_id" : 1, "scores" : [ 1, 5, 3 ] }
{ "_id" : 2, "scores" : [ 8, 17, 18 ] }
{ "_id" : 3, "scores" : [ 15, 11, 8 ] } 次のクエリを実行して、特定の量(この場合は10)よりも高い値を持つ配列要素のみを更新できます。
db.players.update(
{ scores: { $gte: 10 } },
{ $set: { "scores.$[e]" : 10 } },
{ arrayFilters: [ { "e": { $gte: 10 } } ] }
) 結果:
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
予想どおり、2つのドキュメントが条件に一致していても、これは1つのドキュメントのみを更新します(multi: trueを指定しなかったため) 。
現在のドキュメントは次のようになっています。
db.players.find() 結果:
{ "_id" : 1, "scores" : [ 1, 5, 3 ] }
{ "_id" : 2, "scores" : [ 8, 10, 10 ] }
{ "_id" : 3, "scores" : [ 15, 11, 8 ] } ドキュメント2では、2つの配列要素が基準に一致したため、これらの要素が更新されました。
詳細情報
db.collection.update() メソッドは、writeConcernなどの他のパラメータも受け入れます 、collation 、およびhint 。
db.collections.update()については、MongoDBのドキュメントを参照してください。 詳細については。