Overview
このガイドでは、 MongoDB PHPライブラリを使用してMongoDBコレクションに対して置換操作を実行する方法を学習できます。 置換操作は 更新操作とは異なります。 アップデート操作により、ターゲットドキュメント内の指定されたフィールドのみが変更されます。 置換操作により、ターゲットドキュメント内のすべてのフィールドが削除され、新しいフィールドに置き換えられます。
ドキュメントを置き換えるには、 MongoDB\Collection::replaceOne()メソッドを使用します。
サンプル データ
The examples in this guide use the restaurants collection in the sample_restaurants database from the Atlas sample datasets. To access this collection from your PHP application, instantiate a MongoDB\Client that connects to an Atlas cluster and assign the following value to your $collection variable:
$collection = $client->sample_restaurants->restaurants;
無料の MongoDB 配置を作成し、サンプルデータセットを読み込む方法については、MongoDB の スタートガイドを参照してください。
置換操作
MongoDB\Collection::replaceOne()を使用して置換操作を実行できます。 このメソッドは、検索条件に一致する最初のドキュメントから_idフィールドを除くすべてのフィールドを削除します。 次に、指定したフィールドと値がドキュメントに挿入されます。
必要なパラメーター
replaceOne() メソッドには次のパラメーターが必要です。
クエリフィルタードキュメント。置き換えるドキュメントを決定します。 クエリフィルターの詳細については、 MongoDB Serverマニュアルの「クエリフィルター ドキュメント 」セクションを参照してください。
新しいドキュメントに挿入するフィールドと値を指定するドキュメントを置き換えます。
戻り値
replaceOne()メソッドはMongoDB\UpdateResultオブジェクトを返します。 MongoDB\UpdateResult型には次のメソッドが含まれています。
方式 | 説明 |
|---|---|
| アップデートされた数に関係なく、クエリフィルターに一致したドキュメントの数を返します。 |
| 更新操作によって変更されたドキュメントの数を返します。 更新されたドキュメントが元と同一の場合、このカウントには含まれません。 |
| データベースにアップサートされたドキュメントの数を返します(存在する場合)。 |
| ドライバーがアップサートを実行した場合、データベースでアップサートされたドキュメントのIDを返します。 |
| 書込み (write)操作が確認されたかどうかを示すブール値を返します。 |
例
次の例では、 replaceOne()メソッドを使用して、 nameフィールド値が'Pizza Town'であるドキュメントのフィールドと値を置き換えます。 次に、変更されたドキュメントの数を出力します。
$replaceDocument = [ 'name' => 'Mongo\'s Pizza', 'cuisine' => 'Pizza', 'address' => [ 'street' => '123 Pizza St', 'zipCode' => '10003', ], 'borough' => 'Manhattan', ]; $result = $collection->replaceOne(['name' => 'Pizza Town'], $replaceDocument); echo 'Modified documents: ', $result->getModifiedCount();
重要
_idフィールドの値は不変です。 置き換えドキュメントで_idフィールドに値が指定される場合は、既存のドキュメントの_id値と一致する必要があります。
置換操作の変更
オプション値を指定する配列をパラメーターとして渡すことで、 MongoDB\Collection::replaceOne()メソッドの動作を変更できます。 次の表では、 配列に設定できるオプションの一部を説明しています。
オプション | 説明 |
|---|---|
| Specifies whether the replace operation performs an upsert operation if no documents match the query filter. For more information, see the upsert statement in the MongoDB Server manual. |
| Specifies whether the replace operation bypasses document validation. This lets you replace 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. |
| 置換操作を実行する前にドキュメントに適用するソート順序を指定します。 |
| Specifies the kind of language collation to use when sorting results. To learn more, see the Collation section of this page. |
| Gets or sets the index to scan for documents. For more information, see the hint statement in the MongoDB Server manual. |
| 操作に関連付けるクライアントセッションを指定します。 |
| Specifies a document with a list of values to improve operation readability. 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. |
| Attaches a comment to the operation. For more information, see the insert command fields guide in the MongoDB Server manual. |
照合
操作の 照合 を指定するには、collation オプションを設定する $options 配列パラメータを操作メソッドに渡します。照合ルールを構成する配列に collation オプションを割り当てます。
次の表では、照合を構成するために設定できるフィールドについて説明しています。
フィールド | 説明 |
|---|---|
| (Required) Specifies the International Components for Unicode (ICU) locale. For a list of supported locales, see Collation Locales and Default Parameters in the MongoDB Server manual. |
| (任意) 大文字と小文字の比較を含めるかどうかを指定します。 |
| (任意) 三次レベルの比較中の大文字と小文字の相違のソート順序を指定します。 |
| (Optional) Specifies the level of comparison to perform, as defined in the ICU documentation. |
| (任意)ドライバーが数字の string を数値として比較するかどうかを指定します。 |
| (任意) ライブラリが空白と句読点を比較目的の基本文字として考慮するかどうかを指定します。 |
| (任意) |
| (任意) 発音区別符号を含む string を、string の後ろから前にソートするかどうかを指定します。 |
照合と各フィールドに可能な値の詳細については、 MongoDB Serverマニュアルの「 照合 」エントリを参照してください。
例
次のコードでは、 replaceOne()メソッドを使用して、 nameフィールドの値が'Food Town'である最初のドキュメントを検索し、このドキュメントをnameの値が'Food World'である新しいドキュメントに置き換えます。 upsertオプションがtrueに設定されているため、クエリフィルターが既存のドキュメントと一致しない場合、ライブラリは新しいドキュメントを挿入します。
$replaceDocument = [ 'name' => 'Food World', 'cuisine' => 'Mixed', 'address' => [ 'street' => '123 Food St', 'zipCode' => '10003', ], 'borough' => 'Manhattan', ]; $result = $collection->replaceOne( ['name' => 'Food Town'], $replaceDocument, ['upsert' => true], );
詳細情報
アップデート操作の詳細については、ドキュメントのアップデートガイドを参照してください。
クエリフィルターの作成の詳細については、「クエリの指定」ガイドを参照してください。
API ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。