Overview
このガイドでは、 Rubyドライバーを使用して update_one
メソッドと update_many
メソッドを使用してMongoDBコレクション内のドキュメントを更新する方法を学習できます。
サンプル データ
このガイドの例では、 Atlasサンプルデータセット の sample_restaurants
データベースの restaurants
コレクションを使用します。Rubyアプリケーションからこのコレクションにアクセスするには、Atlas クラスターに接続する Mongo::Client
オブジェクトを作成し、次の値を database
変数と collection
変数に割り当てます。
database = client.use('sample_restaurants') collection = database[:restaurants]
MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 「Atlas を使い始める」ガイドを参照してください。
アップデート操作
次の方法を使用して、MongoDB 内のドキュメントを更新できます。
update_one
: 検索条件に一致する最初のドキュメントを更新しますupdate_many
: 検索条件に一致するすべてのドキュメントを更新します
各更新方法には次のパラメーターが必要です。
クエリフィルター 。アップデートするドキュメントに一致します。クエリフィルターの詳細については、「 クエリの指定 」ガイドをご覧ください。
ドキュメント を更新し、更新演算子と更新するフィールドと値を指定します。更新演算子は、実行する更新のタイプを指定します。 更新演算子のリストを表示し、その使用方法については、 MongoDB Serverマニュアルの「 フィールド更新演算子 」のガイドページを参照してください。
1 つのドキュメントの更新例
次の例では、update_one
メソッドを使用して、name
フィールドの値が "Happy Garden"
である最初のドキュメントを検索します。次に、$set
演算子を使用して name
フィールドの値を "Mountain House"
に更新します。
filter = { name: 'Happy Garden' } update = { '$set' => { name: 'Mountain House' } } single_result = collection.update_one(filter, update) puts "#{single_result.modified_count} document(s) updated."
1 document(s) updated
多数のドキュメントの更新例
次の例では、update_many
メソッドを使用して、name
フィールドの値が "Starbucks"
であるすべてのドキュメントを更新します。アップデートドキュメントでは、$rename
演算子を使用して、address
フィールドの名前を location
に変更します。
filter = { name: 'Starbucks' } update = { '$rename' => { address: 'location' } } many_result = collection.update_many(filter, update) puts "#{many_result.modified_count} document(s) updated."
11 document(s) updated
更新操作をカスタマイズする
update_one
メソッドと update_many
メソッドは、 更新操作を構成するための オプションを受け入れます。これらのオプションをパラメーターとして個別に渡すことも、オプションを含む Hash
オブジェクトを作成し、そのオブジェクトをパラメーターとして渡すこともできます。オプションを指定しない場合、ドライバーはデフォルト設定で更新操作を実行します。
次の表では、更新操作を構成するために使用できるオプションについて説明します。
オプション | 説明 |
---|---|
| Whether the update operation performs an upsert operation if no
documents match the query filter. For more information, see the upsert
statement
in the MongoDB Server manual. Default: false |
| 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. Default: false |
| Language collation to use when sorting
results. For more information, see Collation
in the MongoDB Server manual. |
| List of filters that you specify to select which
array elements the update applies to. |
| Index to use when matching documents.
For more information, see the hint statement
in the MongoDB Server manual. |
| 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. For more information,
see the let statement in the
MongoDB Server manual. |
変更更新の例
この例では、$equal
演算子を使用して、name
フィールドの値が "Sunrise Pizzeria"
であるドキュメントを一致させます。次に、$set
演算子を使用して、最初に一致するドキュメントの borough
フィールド値を "Queens"
に設定し、cuisine
フィールド値を "Italian"
に設定します。
upsert
オプションが true
に設定されているため、クエリフィルターが既存のドキュメントと一致しない場合、ドライバーはフィルターと更新ドキュメントのフィールドと値を含む新しいドキュメントを挿入します。
filter = { 'name' => 'Sunrise Pizzeria' } update = { '$set' => { borough: 'Queens', cuisine: 'Italian' } } upsert_result = collection.update_one(filter, update, upsert: true) puts "#{upsert_result.modified_count} document(s) updated."
1 document(s) updated
戻り値
update_one
メソッドと update_many
メソッドはそれぞれ Result
オブジェクトを返します。Result
インスタンスから次のメソッドにアクセスできます。
方式 | 説明 |
---|---|
| Number of documents that matched the query filter, regardless of
how many updates were performed. |
| 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 true if the server acknowledged the result. |
| Returns the number of documents that were upserted in the database, if the driver
performed an upsert. |
| Returns the _id value of the document that was upserted
in the database, if the driver performed an upsert. |
Tip
他の Result
メソッドを呼び出す前に、acknowledged?
メソッドの値を確認してください。acknowledged?
メソッドが false
を返す場合、Result
オブジェクトで他のメソッドを呼び出すと、ドライバーは InvalidOperation
例外をスローします。サーバーが書込み (write)操作を確認しない場合、ドライバーはこれらの値を決定できません。
詳細情報
Rubyドライバーを使用してドキュメントを更新する方法を示す実行可能なコード例については、 MongoDBへのデータの書込み を参照してください。
API ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。