YouTip LogoYouTip

Mongodb Renamecollection

## Renaming Collections in MongoDB In MongoDB, you cannot directly rename a collection using a simple inline update command. Instead, MongoDB provides the `renameCollection` administrative command to safely change the name of an existing collection. The `renameCollection` command is executed against the `admin` database and can rename a collection within the same database or move it to a different database. --- ## Syntax To rename a collection, run the `renameCollection` command using `db.adminCommand()`: ```javascript db.adminCommand({ renameCollection: "sourceDb.sourceCollection", to: "targetDb.targetCollection", dropTarget: }) ``` ### Parameter Details * **`renameCollection`** *(string, required)*: The fully qualified namespace of the source collection (i.e., `databaseName.collectionName`). * **`to`** *(string, required)*: The fully qualified namespace of the target collection (i.e., `databaseName.collectionName`). * **`dropTarget`** *(boolean, optional)*: If set to `true`, MongoDB will drop the existing target collection (if it exists) before renaming. The default value is `false`. --- ## Code Examples ### Example 1: Renaming a Collection Within the Same Database Suppose you want to rename `oldCollection` to `newCollection` inside the `test` database. 1. Switch to your working database: ```javascript use test ``` 2. Run the `renameCollection` command via the `admin` database: ```javascript db.adminCommand({ renameCollection: "test.oldCollection", to: "test.newCollection" }); ``` ### Example 2: Renaming and Moving a Collection to a Different Database You can also use this command to move a collection across databases. For example, to move `oldCollection` from the `test` database to `newCollection` in the `production` database: ```javascript db.adminCommand({ renameCollection: "test.oldCollection", to: "production.newCollection" }); ``` --- ## Verifying the Rename Operation After executing the command, you can verify that the collection has been successfully renamed by listing the collections in the target database: ```javascript use test show collections ``` If the rename was successful, you will see `newCollection` listed in the output. --- ## Handling Rename Failures & Conflicts By default, if the target collection already exists, MongoDB will return an error and the rename operation will fail. If you want to overwrite the existing target collection, you have two options: ### Option 1: Use the `dropTarget` Parameter You can pass `dropTarget: true` in the command to automatically drop the target collection before renaming: ```javascript db.adminCommand({ renameCollection: "test.oldCollection", to: "production.newCollection", dropTarget: true }); ``` ### Option 2: Manually Drop the Target Collection First Alternatively, you can manually drop the target collection before running the rename command: ```javascript // 1. Switch to the target database and drop the existing collection use production db.newCollection.drop(); // 2. Switch back and run the rename command use test db.adminCommand({ renameCollection: "test.oldCollection", to: "production.newCollection" }); ``` --- ## Important Considerations * **Privileges Required**: To execute `renameCollection`, your user account must have appropriate privileges on both the source and target databases. Typically, roles like `dbAdmin` or `dbOwner` are required. * **Indexes and Data**: Renaming a collection is a metadata operation. All documents and existing indexes are fully preserved during the rename. * **Locking Behavior**: The `renameCollection` command obtains an exclusive lock on the source and target collections for the duration of the operation, which blocks other operations on these collections until the rename completes. * **Sharded Collections**: You cannot use `renameCollection` to rename sharded collections. It is only supported for unsharded collections.
← Scala Escape CharRust Smart Pointers β†’