How to create database in MongoDB?

3 Best Ways to Rename MongoDB Database [Rename]

How do you rename a MongoDB database”? 
How to Backup and Restore MongoDB using mongodump and mongorestore”?

3 Ways” to Rename MongoDB Database as,
1.      Rename with Database copy.
2.      Rename with Backup/Restore.
3.      Backup by Shutting down Mongod Instance.

We are using “mongodump” and “mongorestore” commands to Backup and Restore to MongoDB.

Rename with Database Copy:-
In this method, connect to MongoDB with “mongo shell”.

Examples,
> show dbs
admin  0.000GB
local  0.000GB
mydb   0.000GB

> db.copyDatabase("mydb","myNewDB","localhost")
{ "ok" : 1 }

> use mydb
switched to db mydb

> db.dropDatabase()
{ "dropped" : "mydb", "ok" : 1 }

> show dbs
admin    0.000GB
local    0.000GB
myNewDB  0.000GB

> use myNewDB
switched to db myNewDB

> db.users.find()
{"_id" : ObjectId("584f9fca071fb26c09aeb191"), "name" : "Anil Sngh", "site" : "code-sample.com"}
{"_id" : ObjectId("584fb58ea529193a8cc59605"), "name" : "Alok", "site" : "code-view" }
{"_id" : ObjectId("584fb9c5a529193ff41743fd"), "name" : "Alok", "site" : "code-view", 
    "CretaeDate" : ISODate("2016-12-13T09:05:09.627Z") }
{"_id" : ObjectId("585230478c92c30dfd10bed0"), "name" : "Reena", "site" : "www.code-sample.xyz"}

>

Rename with Backup/Restore:-
In this method, the mongodump and mongorestore command will be used to back-up the “old database” and then “restored” into a “new database”.

Examples,
> mongodump old_database_name
> mongorestore --db new_database_name ./dump/old_database_name

Reference links,
I hope you are enjoying with this post! Please share with you friends. Thank you!!

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

www.code-sample.com/. Powered by Blogger.
^