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

MongoDB\Database::renameCollection()

バージョン1.10の新機能。

MongoDB\Database::renameCollection()

現在のデータベース内のコレクションの名前を変更します。

function renameCollection(
string $fromCollectionName,
string $toCollectionName,
?string $toDatabaseName = null,
array $options = []
): array|object
$fromCollectionName : string
名前を変更するコレクションの名前。
$toCollectionName : string
コレクションの新しい名前。
$toDatabaseName : string
コレクションの新しいデータベース名。 新しいデータベース名を指定しない場合は、現在のデータベースが使用されます。 新しい名前で別のデータベースが指定されている場合、コマンドはコレクションを新しいデータベースにコピーし、ソース コレクションを削除します。
$options : 配列

必要なオプションを指定する配列。

名前
タイプ
説明

comment

混合

データベースプロファイラcurrentOp出力、およびログから操作を追跡するのに役立つ任意のコメントを指定できるようにします。

このオプションは MongoDB 4.4 以降で使用可能であり、古いサーバー バージョンで指定すると実行時に例外が発生します。

バージョン1.13の新機能。

dropTarget

ブール値

trueの場合、MongoDB はコレクションの名前を変更する前にターゲットを削除します。 デフォルト値はfalseです。

セッション

操作に関連付けるクライアント セッション。

typeMap

配列

The type map to apply to cursors, which determines how BSON documents are converted to PHP values. Defaults to the database's type map.

これは返されたコマンドの結果ドキュメントに使用されます。

writeConcern

操作に使用する書込み保証 ( write concern )。 データベースの書込み保証 (write concern) のデフォルト。

An array or object with the result document of the renameCollection command. The return type will depend on the typeMap option.

MongoDB\Exception\UnsupportedExceptionオプションが使用され、選択したサーバーでサポートされていない場合(例: collationreadConcernwriteConcern )。

MongoDB\Exception\InvalidArgumentException は、パラメータまたはオプションの解析に関連するエラーの場合は です。

MongoDB$Driver\Exception\RuntimeException は、拡張レベルの他のエラーの場合(例:)。

次の例では、 testデータベース内のrestaurantsコレクションの名前をplacesに変更します。

<?php
$db = (new MongoDB\Client)->test;
$result = $db->renameCollection('restaurants', 'places');
var_dump($result);

出力は次のようになります。

object(MongoDB\Model\BSONDocument)#8 (1) {
["storage":"ArrayObject":private]=>
array(1) {
["ok"]=>
float(1)
}
}