Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Docs Menu
Docs Home
/ / /
PHP ライブラリ マニュアル
/

ドキュメントの置換

項目一覧

  • Overview
  • サンプル データ
  • 置換操作
  • 必要なパラメーター
  • 戻り値
  • 置換操作の変更
  • 詳細情報
  • API ドキュメント

このガイドでは、 MongoDB PHPライブラリを使用してMongoDBコレクションに対して置換操作を実行する方法を学習できます。 置換操作は 更新操作とは異なります。 アップデート操作により、ターゲットドキュメント内の指定されたフィールドのみが変更されます。 置換操作により、ターゲットドキュメント内のすべてのフィールドが削除され、新しいフィールドに置き換えられます。

ドキュメントを置き換えるには、 MongoDB\Collection::replaceOne()メソッドを使用します。

このガイドの例では、 Atlasサンプルデータセットsample_restaurantsデータベースのrestaurantsコレクションを使用します。 PHPアプリケーションからこのコレクションにアクセスするには、Atlas クラスターに接続するMongoDB\Clientをインスタンス化し、 $collection変数に次の値を割り当てます。

$collection = $client->sample_restaurants->restaurants;

MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 「Atlas を使い始める」ガイドを参照してください。

MongoDB\Collection::replaceOne()を使用して置換操作を実行できます。 このメソッドは、検索条件に一致する最初のドキュメントから_idフィールドを除くすべてのフィールドを削除します。 次に、指定したフィールドと値がドキュメントに挿入されます。

replaceOne() メソッドには次のパラメーターが必要です。

  • クエリフィルタードキュメント。置き換えるドキュメントを決定します。 クエリフィルターの詳細については、 MongoDB Serverマニュアルの「クエリフィルター ドキュメント 」セクションを参照してください。

  • 新しいドキュメントに挿入するフィールドと値を指定するドキュメントを置き換えます。

replaceOne()メソッドはMongoDB\UpdateResultオブジェクトを返します。 MongoDB\UpdateResult型には次のメソッドが含まれています。

方式
説明

getMatchedCount()

Returns the number of documents that matched the query filter, regardless of how many were updated.

getModifiedCount()

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.

getUpsertedCount()

Returns the number of documents upserted into the database, if any.

getUpsertedId()

Returns the ID of the document that was upserted in the database, if the driver performed an upsert.

isAcknowledged()

Returns a boolean indicating whether the write operation was acknowledged.

次の例では、 replaceOne()メソッドを使用して、 nameフィールド値が'Pizza Town'であるドキュメントのフィールドと値を置き換えます。 次に、変更されたドキュメントの数を出力します。

$replace_document = [
'name' => 'Mongo\'s Pizza',
'cuisine' => 'Pizza',
'address' => [
'street' => '123 Pizza St',
'zipCode' => '10003',
],
'borough' => 'Manhattan'
];
$result = $collection->replaceOne(['name' => 'Pizza Town'], $replace_document);
echo 'Modified documents: ', $result->getModifiedCount();
Modified documents: 1

重要

_idフィールドの値は不変です。 置き換えドキュメントで_idフィールドに値が指定される場合は、既存のドキュメントの_id値と一致する必要があります。

オプション値を指定する配列をパラメーターとして渡すことで、 MongoDB\Collection::replaceOne()メソッドの動作を変更できます。 次の表では、 配列に設定できるオプションの一部を説明しています。

オプション
説明

upsert

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.
Defaults to false.

bypassDocumentValidation

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.
Defaults to false.

sort

Specifies the sort order to apply to documents before performing the replace operation.

collation

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

hint

Gets or sets the index to scan for documents. For more information, see the hint statement in the MongoDB Server manual.

session

Specifies the client session to associate with the operation.

let

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.

comment

Attaches a comment to the operation. For more information, see the insert command fields guide in the MongoDB Server manual.

次のコードでは、 replaceOne()メソッドを使用して、 nameフィールドの値が'Food Town'である最初のドキュメントを検索し、このドキュメントをnameの値が'Food World'である新しいドキュメントに置き換えます。 upsertオプションがtrueに設定されているため、クエリフィルターが既存のドキュメントと一致しない場合、ライブラリは新しいドキュメントを挿入します。

$replace_document = [
'name' => 'Food World',
'cuisine' => 'Mixed',
'address' => [
'street' => '123 Food St',
'zipCode' => '10003',
],
'borough' => 'Manhattan'
];
$result = $collection->replaceOne(
['name' => 'Food Town'],
$replace_document,
['upsert' => true]
);

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

クエリフィルターの作成の詳細については、「クエリの指定」ガイドを参照してください。

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

戻る

削除