MongoDB とドライバー
定義
db.collection.renameCollection(target, dropTarget)コレクションの名前を変更します。
renameCollectionデータベースコマンドのラッパーを提供します。
パラメーター
Parameter | タイプ | 説明 |
|---|---|---|
| string | コレクションの新しい名前。文字列を引用符で囲みます。詳細は、「命名制限」を参照してください。 |
| ブール値 | 任意。 |
動作
The db.collection.renameCollection() method operates within a collection by changing the metadata associated with a given collection.
追加の警告とメッセージについては、ドキュメント「renameCollection」を参照してください。
警告
The db.collection.renameCollection() method and renameCollection command will invalidate open cursors which interrupts queries that are currently returning data.
For MongoDB Change Streams, the db.collection.renameCollection() method and renameCollection command create an invalidate Event for any existing MongoDB Change Streams opened on the source or target collection.
この方法には、次の制限があります。
db.collection.renameCollection()cannot move a collection between databases. UserenameCollectionfor these rename operations.ビューの名前を変更することはできません。
db.collection.renameCollection()is not supported on time series collections.コレクションの名前をそれ自体に変更することはできません。コレクションの名前をそれ自体に変更しようとすると、
IllegalOperationエラーがスローされます。
シャーディングされたクラスターでのリソース ロック
バージョン5.0で変更。
シャーディングされたクラスター内のシャーディングされたコレクションまたはシャーディングされていないコレクションの名前を変更すると、ソース コレクションとターゲット コレクションだけがすべてのシャードでロックされます。ソース コレクションとターゲット コレクションでのその後の操作は、名前変更操作が完了するまで待つ必要があります。
MongoDB でのロックの詳細については、「FAQ: 同時実行性」を参照してください。
レプリカセットでのリソース ロック
renameCollection() obtains an exclusive lock on the source and target collections for the duration of the operation. All subsequent operations on the collections must wait until renameCollection() completes.
相互作用: mongodump
A mongodump started with --oplog fails if a client issues db.collection.renameCollection() during the dump process. See mongodump.--oplog for more information.
例
Call the db.collection.renameCollection() method on a collection object. For example:
db.rrecord.renameCollection("record")
この操作により、 rrecordコレクションの名前がrecordに変更されます。ターゲット名(つまり、record )が既存のコレクションの名前である場合、操作は失敗します。