Overview
このガイドでは、 Rubyドライバーを使用してMongoDBコレクション内のドキュメントに対して置換操作を実行する方法を学習できます。置換操作と、指定されたドキュメントから _id
フィールドを除くすべてのフィールドと値が削除され、指定した新しいフィールドと値が追加されます。この操作は、1 つ以上のドキュメントの指定されたフィールドのみを変更する 更新操作とは異なります。
更新操作について詳しくは、 ドキュメントの更新のガイドを参照してください。
サンプル データ
このガイドの例では、 Atlasサンプルデータセット restaurants
sample_restaurants
の データベースの コレクションを使用します。 Rubyアプリケーションからこのコレクションにアクセスするには、AtlasMongo::Client
クラスターに接続する オブジェクトを作成し、次の値を 変数と 変数に割り当てます。database
collection
database = client.use('sample_restaurants') collection = database[:restaurants]
MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 「Atlas を使い始める」ガイドを参照してください。
置換操作
MongoDBでは、replace_one
メソッドを使用して置換操作を実行できます。 このメソッドは、指定されたクエリフィルターに一致する最初のドキュメントから _id
フィールドを除くすべてのフィールドを削除します。 次に、指定したフィールドと値が空のドキュメントに追加されます。
必要なパラメーター
次のパラメータをreplace_one
メソッドに渡す必要があります。
クエリフィルター : 更新するドキュメントを指定します。クエリフィルターの詳細については、「 クエリの指定 」ガイドを参照してください。
置換ドキュメント: 既存のフィールドと値を置き換えるフィールドと値を指定します。
置き換えの例
次の例では、 replace_one
メソッドを使用して、name
フィールドの値が "Primola Restaurant"
であるドキュメントのフィールドと値を置き換えます。
filter = { name: 'Primola Restaurant' } new_document = { name: 'Frutti Di Mare', cuisine: 'Seafood', borough: 'Queens' } result = collection.replace_one(filter, new_document) puts "Replaced #{result.modified_count} document(s)"
Replaced 1 document(s)
重要
_id
フィールドの値は不変です。 置き換えドキュメントで_id
フィールドの値を指定する場合、既存のドキュメントの _id
値と同じである必要があります。そうでない場合、ドライバーは WriteError
を発生させます。
置換操作をカスタマイズする
置換操作を構成するオプションを設定するには、Hash
オブジェクトをreplace_one
メソッドのパラメーターとして渡します。オプションを指定しない場合、ドライバーはデフォルト設定で置換操作を実行します。
次の表では、置換操作を構成するために使用できるオプションについて説明します。
オプション | 説明 |
---|---|
| Specifies whether the replace operation performs an upsert operation if no
documents match the query filter. For more information, see upsert
behavior
in the MongoDB Server manual. Defaults to false . |
| Specifies whether the update operation bypasses document validation. This lets you
update documents that don't meet the schema validation requirements, if any
exist. For more information about schema validation, see Schema
Validation in the MongoDB Server manual. Defaults to false . |
| Specifies the kind of language collation to use when sorting
results. For more information, see Collation
in the MongoDB Server manual. |
| Specifies the session to use for the operation. To learn more about sessions, see
Client Sessions and Causal Consistency Guarantees
in the MongoDB Server manual. |
| Sets the index to use when matching documents.
For more information, see the hint statement
in the MongoDB Server manual. |
| Provides a map of parameter names and values to set top-level
variables for the operation. Values must be constant or closed
expressions that don't reference document fields. |
次のコードは、前の例と同じ置換操作を実行しますが、upsert
オプションを true
に設定します。これは、クエリフィルターが既存のドキュメントと一致しない場合、置換ドキュメントで指定されたフィールドと値を含む新しいドキュメントを挿入するようにドライバーに指示します。
options = { upsert: true } result = collection.replace_one(filter, new_document, options) puts "Replaced #{result.upserted_count} document(s)"
Replaced 1 document(s)
戻り値
replace_one
メソッドは Mongo::Operation::Update::Result
オブジェクトを返します。 Result
インスタンスからの情報にアクセスするには、次のメソッドを使用できます。
方式 | 説明 |
---|---|
| Returns the number of documents that matched the query filter. |
| Returns the number of documents modified by the update operation. If an updated
document is identical to the original, it is not included in this
count. |
| Returns the number of documents upserted. |
| Returns the _id value of the document that the driver upserted
into the database, if any. |
詳細情報
ドキュメントを置き換える方法を示す実行可能なコード例については、「 MongoDB へのデータの書込み 」を参照してください。
API ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。