Overview
このガイドでは、 MongoDB PHPライブラリを使用して、削除操作を実行し、 MongoDBコレクションからドキュメントを削除する方法を学習できます。
削除操作は、MongoDB コレクションから 1 つ以上のドキュメントを削除します。 削除操作は、 MongoDB\Collection::deleteOne()
またはMongoDB\Collection::deleteMany()
メソッドを使用して実行できます。
サンプル データ
このガイドの例では、 Atlasサンプルデータセットのsample_restaurants
データベースのrestaurants
コレクションを使用します。 PHPアプリケーションからこのコレクションにアクセスするには、Atlas クラスターに接続するMongoDB\Client
をインスタンス化し、 $collection
変数に次の値を割り当てます。
$collection = $client->sample_restaurants->restaurants;
MongoDB Atlasクラスターを無料で作成して、サンプルデータセットをロードする方法については、 「Atlas を使い始める」ガイドを参照してください。
削除操作
次の方法を使用して削除操作を実行できます。
MongoDB\Collection::deleteOne()
は、検索条件に一致する最初のドキュメントを削除します。MongoDB\Collection::deleteMany()
は、検索条件に一致するすべてのドキュメントを削除します
各削除メソッドにはクエリフィルタードキュメントが必要です。このドキュメントは、削除対象として選択するドキュメントを決定するための検索条件を指定します。 クエリフィルターの詳細については、 MongoDB Serverマニュアルの「クエリフィルター ドキュメント 」セクションを参照してください。
単一ドキュメントの削除
次の例では、 deleteOne()
メソッドを使用して、 name
の値が'Ready Penny Inn'
であるrestaurants
コレクション内のドキュメントを削除します。
$collection->deleteOne(['name' => 'Ready Penny Inn']);
複数のドキュメントの削除
次の例では、 deleteMany()
メソッドを使用して、 borough
の値が'Brooklyn'
であるrestaurants
コレクション内のすべてのドキュメントを削除します。
$collection->deleteMany(['borough' => 'Brooklyn']);
削除操作の変更
オプション値を指定する配列をパラメーターとして渡すことで、 MongoDB\Collection::deleteOne()
メソッドとMongoDB\Collection::deleteMany()
メソッドの動作を変更できます。 次の表では、 配列に設定できるオプションについて説明しています。
オプション | 説明 |
---|---|
| Specifies the kind of language collation to use when comparing
strings. To learn more, see the Collation section
of this page. |
| Sets the write concern for the operation. This option defaults to
the collection's write concern.
For more information, see Write Concern
in the MongoDB Server manual. |
| 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. |
| Specifies the client session to associate with the operation. For more
information, see Session in the
MongoDB Server manual. |
| Attaches a comment to the operation. For more information, see the delete 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. Data Type: string |
| (Optional) Specifies whether to include case comparison. When set to true , the comparison behavior depends on the value of
the strength field:- If strength is 1 , the PHP library compares basecharacters and case. - If strength is 2 , the PHP library compares basecharacters, diacritics, other secondary differences, and case. - If strength is any other value, this field is ignored.When set to false , the PHP library doesn't include case comparison at
strength level 1 or 2 .Data Type: bool Default: false |
| (Optional) Specifies the sort order of case differences during tertiary
level comparisons. Data Type: string Default: "off" |
| (Optional) Specifies the level of comparison to perform, as defined in the
ICU documentation. Data Type: int Default: 3 |
| (Optional) Specifies whether the driver compares numeric strings as numbers. If set to true , the PHP library compares numeric strings as numbers.
For example, when comparing the strings "10" and "2", the library uses the
strings' numeric values and treats "10" as greater than "2".If set to false , the PHP library compares numeric strings
as strings. For example, when comparing the strings "10" and "2", the library
compares one character at a time and treats "10" as less than "2".For more information, see Collation Restrictions
in the MongoDB Server manual. Data Type: bool Default: false |
| (Optional) Specifies whether the library considers whitespace and punctuation as base
characters for comparison purposes. Data Type: string Default: "non-ignorable" |
| (Optional) Specifies which characters the library considers ignorable when
the alternate field is set to "shifted" .Data Type: string Default: "punct" |
| (Optional) Specifies whether strings containing diacritics sort from the back of the string
to the front. Data Type: bool Default: false |
照合と各フィールドに可能な値の詳細については、 MongoDB Serverマニュアルの「 照合 」エントリを参照してください。
例
次の例では、 deleteMany()
メソッドを呼び出して、 name
値に string 'Mongo'
が含まれるrestaurants
コレクション内のすべてのドキュメントを削除します。 また、配列パラメータのcomment
オプションを設定して、操作にコメントを追加します。
$collection->deleteMany( ['name' => new MongoDB\BSON\Regex('Mongo')], ['comment' => 'Deleting Mongo restaurants'], );
注意
前述の例でdeleteMany()
メソッドをdeleteOne()
に置き換えると、ライブラリはname
値に'Mongo'
を含む最初のドキュメントのみを削除します。
戻り値
MongoDB\Collection::deleteOne()
メソッドとMongoDB\Collection::deleteMany()
メソッドはMongoDB\DeleteResult
オブジェクトを返します。 このクラスには、次のメンバー関数が含まれています。
isAcknowledged()
は、操作が確認されたかどうかを示すブール値を返します。getDeletedCount()
は、削除されたドキュメントの数を返します。 書込み (write)操作が確認されなかった場合、このメソッドはエラーをスローします。
クエリフィルターがどのドキュメントにも一致しない場合、ドライバーはドキュメントを削除せず、 getDeletedCount()
は0
を返します。
例
次の例では、 deleteMany()
メソッドを呼び出して、 cuisine
の値が'Greek'
であるドキュメントを削除します。 次に、 getDeletedCount()
ノード関数を呼び出して、削除されたドキュメント数を出力します。
$result = $collection->deleteMany(['cuisine' => 'Greek']); echo 'Deleted documents: ', $result->getDeletedCount(), PHP_EOL;
Deleted documents: 111
API ドキュメント
このガイドで説明したメソッドや型の詳細については、次の API ドキュメントを参照してください。