MongoDBでは、db.collection.updateMany() メソッドは、コレクションに指定されたフィルターに一致するすべてのドキュメントを更新します。
例
petsというコレクションがあるとします。 次のドキュメントが含まれています:
{ "_id" : 1, "name" : "Wag", "type" : "Dog" }
{ "_id" : 2, "name" : "Bark", "type" : "Dog" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }
2つのドキュメントにDogがあることがわかります typeとして 。
次のように、両方のドキュメントを一度に更新できます:
db.pets.updateMany(
{ type: "Dog" },
{ $set: { type: "Cow" } }
) 結果:
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 } これは、2つのドキュメントが一致し、2つが更新されたことを示しています。
コレクションを確認できます:
db.pets.find() 結果:
{ "_id" : 1, "name" : "Wag", "type" : "Cow" }
{ "_id" : 2, "name" : "Bark", "type" : "Cow" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" } アップサート
db.collection.updateMany() メソッドはupsertを受け入れます アップサート操作を実行できるようにする引数。
upsert: trueの場合 、フィルタ条件に一致するすべてのドキュメントが更新されますが、一致するドキュメントがない場合は、新しいドキュメントが挿入されます。
元のドキュメントからもう一度始めましょう:
{ "_id" : 1, "name" : "Wag", "type" : "Dog" }
{ "_id" : 2, "name" : "Bark", "type" : "Dog" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" } 例:
db.pets.updateMany(
{ name: "Bubbles" },
{ $set: { type: "Fish" } },
{ upsert: true }
) 結果:
{
"acknowledged" : true,
"matchedCount" : 0,
"modifiedCount" : 0,
"upsertedId" : ObjectId("5fe27e1dd991410169410244")
} この場合、一致するものがなかったため、ドキュメントがアップサートされました。
コレクションを確認しましょう。
db.pets.find() 結果:
{ "_id" : 1, "name" : "Wag", "type" : "Dog" }
{ "_id" : 2, "name" : "Bark", "type" : "Dog" }
{ "_id" : 3, "name" : "Meow", "type" : "Cat" }
{ "_id" : ObjectId("5fe27e1dd991410169410244"), "name" : "Bubbles", "type" : "Fish" } 埋め込みドキュメント
db.collection.updateMany()を使用することもできます 埋め込まれたドキュメントを更新します。
次のドキュメントを挿入するとします。
db.pets.insertMany([
{
"_id" : 1,
"name" : "Wag",
"type" : "Dog",
"specs" : {
"height" : 400,
"weight" : 15,
"color" : "white"
}
},
{
"_id" : 2,
"name" : "Bark",
"type" : "Dog",
"specs" : {
"height" : 200,
"weight" : 12,
"color" : "white"
}
}
]) 次のコードを使用して、埋め込まれたドキュメントを更新できます。
db.pets.updateMany({
type: "Dog"
}, {
$set: {
"specs.color": "brown",
"specs.gooddog": false
}
}) 結果:
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 } したがって、両方のドキュメントが更新されたことがわかります。
ドキュメントを確認しましょう。
db.pets.find({
type: "Dog"
}).pretty() 結果:
{
"_id" : 1,
"name" : "Wag",
"type" : "Dog",
"specs" : {
"height" : 400,
"weight" : 15,
"color" : "brown",
"gooddog" : false
}
}
{
"_id" : 2,
"name" : "Bark",
"type" : "Dog",
"specs" : {
"height" : 200,
"weight" : 12,
"color" : "brown",
"gooddog" : false
}
} 埋め込まれたドキュメントが指定どおりに更新されたことがわかります。
配列
db.collection.updateMany()を使用しましょう アレイを更新します。
playersというコレクションがあるとします。 次のドキュメントを使用:
{ "_id" : 1, "scores" : [ 1, 5, 3 ] }
{ "_id" : 2, "scores" : [ 8, 17, 18 ] }
{ "_id" : 3, "scores" : [ 15, 11, 8 ] } すべてのドキュメントの2つの配列要素を更新しましょう。
db.players.updateMany({},
{
$set: {
"scores.0": 20,
"scores.1": 26
}
}) 結果:
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
3つのドキュメントすべてが更新されたことがわかります。これは、フィルター基準を空白のままにしたためです({}を使用しました) フィルタ基準について)。
そして、ドキュメントを見てみましょう。
db.players.find() 結果:
{ "_id" : 1, "scores" : [ 20, 26, 3 ] }
{ "_id" : 2, "scores" : [ 20, 26, 18 ] }
{ "_id" : 3, "scores" : [ 20, 26, 8 ] } arrayFiltersパラメーター
arrayFiltersを使用することもできます パラメータと位置の$ 更新する配列要素を決定する演算子。
たとえば、以前のドキュメントを見てください:
{ "_id" : 1, "scores" : [ 20, 26, 3 ] }
{ "_id" : 2, "scores" : [ 20, 26, 18 ] }
{ "_id" : 3, "scores" : [ 20, 26, 8 ] } 次のクエリを実行して、特定の量(この場合は15)よりも高い値を持つ配列要素のみを更新できます。
db.players.updateMany(
{ scores: { $gte: 15 } },
{ $set: { "scores.$[e]" : 15 } },
{ arrayFilters: [ { "e": { $gte: 15 } } ] }
) 結果:
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 } 予想どおり、すべてのドキュメントが基準に一致するため、更新されます。ただし、すべての配列要素が更新されたわけではありません。
現在のドキュメントは次のようになっています。
db.players.find() 結果:
{ "_id" : 1, "scores" : [ 15, 15, 3 ] }
{ "_id" : 2, "scores" : [ 15, 15, 15 ] }
{ "_id" : 3, "scores" : [ 15, 15, 8 ] } 更新された配列要素は、値が15を超えるものだけでした。
詳細情報
db.collection.updateMany() メソッドは、writeConcernなどの他のパラメータも受け入れます 、collation 、およびhint 。
db.collections.updateMany()については、MongoDBのドキュメントを参照してください。 詳細については。