AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

Delete Documents

このガイドでは、 MongoDB PHPライブラリを使用して、削除操作を実行し、 MongoDBコレクションからドキュメントを削除する方法を学習できます。

削除操作は、MongoDB コレクションから 1 つ以上のドキュメントを削除します。 削除操作は、 MongoDB\Collection::deleteOne()またはMongoDB\Collection::deleteMany()メソッドを使用して実行できます。

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::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()メソッドの動作を変更できます。 次の表では、 配列に設定できるオプションについて説明しています。

オプション
説明

collation

Specifies the kind of language collation to use when comparing strings. To learn more, see the Collation section of this page.

writeConcern

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.

hint

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

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.

session

Specifies the client session to associate with the operation. For more information, see Session in the MongoDB Server manual.

comment

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

操作の 照合 を指定するには、collation オプションを設定する $options 配列パラメータを操作メソッドに渡します。照合ルールを構成する配列に collation オプションを割り当てます。

次の表では、照合を構成するために設定できるフィールドについて説明しています。

フィールド
説明

locale

(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

caseLevel

(任意) 大文字と小文字の比較を含めるかどうかを指定します。

truestrengthに設定すると、比較の動作は

strength1
フィールドの値に応じて変わります。 - が の場合、PHP ライブラリは基本文字と大文字と小文字を比較します。

-strength2 の場合、PHP
ライブラリは基本文字、分音符号、その他のセカンダリの違い、および大文字と小文字を比較します。

-strength がその他の値の場合、このフィールドは無視されます。

false に設定すると、PHP ライブラリは強度レベル 1 または 2 での大文字と小文字の比較を含めません。

データ型: bool
デフォルト: false

caseFirst

(任意) 三次レベルの比較中の大文字と小文字の相違のソート順序を指定します。

データ型: string
デフォルト: "off"

strength

(Optional) Specifies the level of comparison to perform, as defined in the ICU documentation.

Data Type: int
Default: 3

numericOrdering

(任意)ドライバーが数字の string を数値として比較するかどうかを指定します。

trueに設定されている場合、 PHPライブラリは数字の string を数値として比較します。例は、""10 2という文字列を比較する場合、および10 "q" では、ライブラリは string の数値を使用し、""2 として「

」より大きい場合、false に設定されている場合、 PHPライブラリは数字の string を string として比較します。例は、"" という文字列を比較する場合、および10 2"q" では、ライブラリは一度に 110 文字ずつ比較し、""2 を処理します「

」未満であるため、詳細については、 MongoDB Serverマニュアルの「 照合制限

」を参照してください。データ型:bool
デフォルト:false

alternate

(任意) ライブラリが空白と句読点を比較目的の基本文字として考慮するかどうかを指定します。

データ型: string
デフォルト: "non-ignorable"

maxVariable

(任意) alternate フィールドが "shifted" に設定されている場合、ライブラリが無視できる文字を指定します。

データ型: string
デフォルト: "punct"

backwards

(任意) 発音区別符号を含む string を、string の後ろから前にソートするかどうかを指定します。

データ型: bool
デフォルト: 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;

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