MongoDBデータ変更の手段
MongoDBでは、データを修正する方法は次のとおりです。
- updateOne()
db.collection.updateOne(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>
}
)
- updateMany()
db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>
}
)
- replaceOne()
db.collection.replaceOne(
<filter>,
<replacement>,
{
upsert: <boolean>,
writeConcern: <document>
}
)
- 1 件ドキュメントを見つけて更新します。
db.collection.findOneAndUpdate(
<filter>,
<update>,
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
upsert: <boolean>,
returnNewDocument: <boolean>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ],
hint: <document|string>
}
)
- ドキュメントを見つけ出して置き換える
db.collection.findOneAndReplace(
<filter>,
<replacement>,
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
upsert: <boolean>,
returnNewDocument: <boolean>,
collation: <document>,
hint: <document|string>
}
)
- findOneAndDelete()
db.collection.findOneAndDelete(
<filter>,
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
collation: <document>,
hint: <document|string>
}
)
上記のメソッドでは、パラメータはフィルタ条件を指定するために使用され、パラメータは実行される修正操作を指定するために使用され、パラメータは置き換えられるドキュメントを指定するために使用されます。