Docs Menu
Docs Home
/ / /
Ruby ドライバー
/

ドキュメントの置換

このガイドでは、 Rubyドライバーを使用してMongoDBコレクション内のドキュメントに対して置換操作を実行する方法を学習できます。置換操作と、指定されたドキュメントから _idフィールドを除くすべてのフィールドと値が削除され、指定した新しいフィールドと値が追加されます。この操作は、1 つ以上のドキュメントの指定されたフィールドのみを変更する 更新操作とは異なります。

更新操作について詳しくは、 ドキュメントの更新のガイドを参照してください。

このガイドの例では、 Atlasサンプルデータセット restaurantssample_restaurantsの データベースの コレクションを使用します。 Rubyアプリケーションからこのコレクションにアクセスするには、AtlasMongo::Client クラスターに接続する オブジェクトを作成し、次の値を 変数と 変数に割り当てます。databasecollection

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 メソッドのパラメーターとして渡します。オプションを指定しない場合、ドライバーはデフォルト設定で置換操作を実行します。

次の表では、置換操作を構成するために使用できるオプションについて説明します。

オプション
説明

upsert

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.

bypass_document_validation

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.

collation

Specifies the kind of language collation to use when sorting results. For more information, see Collation in the MongoDB Server manual.

session

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.

hint

Sets the index to use when matching documents. For more information, see the hint statement in the MongoDB Server manual.

let

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インスタンスからの情報にアクセスするには、次のメソッドを使用できます。

方式
説明

matched_count

Returns the number of documents that matched the query filter.

modified_count

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.

upserted_count

Returns the number of documents upserted.

upserted_id

Returns the _id value of the document that the driver upserted into the database, if any.

ドキュメントを置き換える方法を示す実行可能なコード例については、「 MongoDB へのデータの書込み 」を参照してください。

このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。

戻る

ドキュメントの挿入

項目一覧