Docs Menu

db.collection.renameCollection()

MongoDB とドライバー

このページではmongoshメソッドがドキュメントされています。 MongoDB ドライバーで同等のメソッドを確認するには、プログラミング言語の対応するページを参照してください。

db.collection.renameCollection(target, dropTarget)

Renames a collection. Provides a wrapper for the renameCollection データベースコマンド.

Parameter
タイプ
説明

target

string

The new name of the collection. Enclose the string in quotes. See 命名制限.

dropTarget

ブール値

Optional. If true, mongod drops the target of renameCollection prior to renaming the collection. The default value is false.

このメソッドは、次の環境でホストされている配置で使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです

注意

このコマンドは、すべての MongoDB Atlas クラスターでサポートされています。すべてのコマンドに対する Atlas のサポートについては、「サポートされていないコマンド」を参照してください。

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

The db.collection.renameCollection() method operates within a collection by changing the metadata associated with a given collection.

Refer to the documentation renameCollection for additional warnings and messages.

警告

The db.collection.renameCollection() method and renameCollection command invalidate open cursors. This creates an invalidate event for any existing change streams opened on the source or target collection, and also interrupts queries that are currently returning data from the renamed collection.

  • The method has the following limitations:

    • db.collection.renameCollection() cannot move a collection between databases. Use renameCollection for these rename operations.

    • ビューの名前を変更することはできません。

    • db.collection.renameCollection() is not supported on time series collections.

    • You cannot rename a collection to itself. If you try to rename a collection to itself an IllegalOperation error is thrown.

バージョン 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.

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")

This operation will rename the rrecord collection to record. If the target name (i.e. record) is the name of an existing collection, then the operation will fail.