Renaming a Database in MongoDB
With the help of this MongoDB tutorial article, you will learn how to rename a MongoDB database. You will achieve this through two methods.
The following method renames a database in MongoDB.
- Rename a MongoDB database using the MongoDB GUI
- Rename the MongoDB database using the MongoDB shell
Rename a MongoDB database using the MongoDB GUI
The following procedure demonstrates how to rename a database in MongoDB using the MongoDB GUI.
- Duplicate the original database by copying all collections, views, and buckets.
- Create a new database.
- Name the new database whatever you want.
- Paste the copied collections, views, and buckets into the new database.
- Delete the original database.
In this example, you rename the database user to customers.
(Step 1) Copy the original MongoDB database
Once connected to your MongoDB instance, you will notice a list of databases in the connection tree on the left.
Studio 3T's sophisticated context menus eliminate the need to use the shell to perform basic MongoDB CRUD tasks. In this case, all we want to do is copy the contents of the user database.
You can do this by following the steps below.
- Right-click User Databases.
- Click Copy All Collections/Views/Buckets.
If you don't see any copy options, make sure you don't have more than one node selected.
(Step 2) Create a new MongoDB database
Next, you will create a new database where you can paste the copied content. To add a database, follow the steps below.
- Right-click the target server (in our example, the Studio 3T replica set).
- Select Add Database....
(Step 3) Name the new database
The Add Database dialog box will pop up. You can name the newly created database customer and click OK.
The customer database will then pop up in the connection tree.
(Step 4) Paste the contents of the original database
Now that you have created the new database, you will follow the below given steps to paste the contents of the original database users into customers.
- Right-click the Customer database.
- Select Paste Collection/View/Bucket.
The time this process takes depends on the size of the original database. Progress is always tracked in the operation window, which is located below the connection tree in the lower left corner.
(Step 5) Delete the original MongoDB database
Once you have ensured that everything has been copied correctly to the new database, you can delete the original database user.
- Right-click the original database.
- Select Delete database.
- Click Delete Database.
If you do not have the appropriate user permissions, Studio 3T will display an error notification.
The original database should disappear from the connection tree.
Rename the MongoDB database using the MongoDB Shell
Another method that can be used to rename a MongoDB database is by using the MongoDB shell. Before renaming a database, make sure to back up the database first and confirm the new database before deleting the old one.
Rename using database copy
Connect to MongoDB using the mongo shell.
mongo
The old database contents can be copied to the new database.
db.copyDatabase('old_database', 'new_database')
Before deleting the old database, it is a good idea to double-check that replication was successful. You can delete the old database.
use old_database
db.dropDatabase()
Rename using backup/restore
The mongodump and mongorestore commands can back up an existing database and then restore it into a database with a new name.
mongodump old_database
mongorestore --db new_database ./dump/old_database
Once the copy is confirmed, you can delete the old database. Connect to MongoDB using the mongo shell.
mongo
The old database will be deleted.
use old_database
db.dropDatabase()
With the help of this MongoDB article, we have learned how to rename MongoDB database using different methods. These methods are rename MongoDB database using MongoDB GUI and rename MongoDB database using MongoDB shell.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
$ne operator in MongoDB
Publish Date:2025/04/11 Views:84 Category:MongoDB
-
This article will discuss how the $ne operator works in MongoDB. In addition, we will list its differences from the $not operator. $ne operator in MongoDB $ne is an operator in MongoDB that stands for not equal to. This will compare the val
MongoDB $Set Operator
Publish Date:2025/04/11 Views:159 Category:MongoDB
-
With the help of this article, you will learn how to use $set the operator to partially update objects in MongoDB so that the new object overlaps/merges with the existing object. The $set operator replaces the value of a field with a given
Difference between $push and $addToSet in MongoDB
Publish Date:2025/04/11 Views:63 Category:MongoDB
-
This article explains the operators in MongoDB. What is the purpose of $push and $addToSet operators. Furthermore, the difference between these two operators is given in the code snippet. This article discusses the following topics. Operato
Sort a collection by date in MongoDB
Publish Date:2025/04/11 Views:64 Category:MongoDB
-
In this MongoDB tutorial, the problem of sorting a collection in MongoDB is discussed. The different ways to sort a collection in the database are briefly explained. Using sort() function in MongoDB This problem is solved using the MongoDB
Counting records in MongoDB
Publish Date:2025/04/11 Views:146 Category:MongoDB
-
This article discusses operators in MongoDB, aggregation operators, and different ways to calculate the total number of records. Operations in MongoDB CRUD operations are a user interface concept that allows users to browse, search, and cha
Pretty printing in MongoDB
Publish Date:2025/04/11 Views:150 Category:MongoDB
-
This article will discuss how to use pretty printing in MongoDB to display formatted results. Pretty printing in MongoDB A cursor is an object that allows programmers in the Mongo world to iterate over documents in a Mongo collection. Altho
MongoDB Adding Elements to an Array
Publish Date:2025/04/11 Views:136 Category:MongoDB
-
This article will cover the various ways to add to an array in MongoDB. Adding to an array in MongoDB Use the $push operator to add values to an array The $push operator is one of the various array update operators provided by MongoDB
MongoDB Search by ID
Publish Date:2025/04/11 Views:131 Category:MongoDB
-
The following article provides an overview of MongoDB find by Id() method. MongoDB provides a find by Id() function which can retrieve documents matching a user id. To use search by Id() in MongoDB, you need to use the find() function. If n
MongoDB starts with a query
Publish Date:2025/04/10 Views:195 Category:MongoDB
-
In this MongoDB article, users will learn how to start queries using $regex. It provides regular expression functionality for pattern matching strings in queries. MongoDB starts querying using $regex If you want to use $regex , use one of t