[{"data":1,"prerenderedAt":308},["ShallowReactive",2],{"i-lucide:sun-moon":3,"i-lucide:menu":8,"post-\u002Fblog\u002F2024-05-08-howto-migrate-a-monogdb":10,"surround-\u002Fblog\u002F2024-05-08-howto-migrate-a-monogdb":291,"i-lucide:arrow-left":302,"i-lucide:sparkles":304,"i-lucide:user":306},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M12 2v2m2.837 12.385a6 6 0 1 1-7.223-7.222c.624-.147.97.66.715 1.248a4 4 0 0 0 5.26 5.259c.589-.255 1.396.09 1.248.715M16 12a4 4 0 0 0-4-4m7-3l-1.256 1.256M20 12h2\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M4 5h16M4 12h16M4 19h16\"\u002F>",{"id":11,"title":12,"authors":13,"body":16,"date":277,"description":278,"extension":279,"meta":280,"navigation":283,"path":284,"seo":285,"stem":286,"tags":287,"toc":6,"__hash__":290},"blog\u002Fblog\u002F2024-05-08-howto-migrate-a-monogdb.md","How to Migrate Data from One MongoDB Database to Another",[14,15],"gpt","pfarmer",{"type":17,"value":18,"toc":266},"minimark",[19,32,37,40,57,60,67,76,100,103,124,127,133,145,169,172,201,210,214,255,259,262],[20,21,22,23,27,28,31],"p",{},"When working with MongoDB, there are times when you need to migrate data from one database to another. This could be for backup purposes, testing, or moving a production database to a new environment. In this post, I'll guide you through the process of dumping data from a MongoDB database named ",[24,25,26],"code",{},"db1"," and restoring it to another database named ",[24,29,30],{},"db2",". This is a common task for database administrators and developers alike.",[33,34,36],"h2",{"id":35},"overview-of-tools-used","Overview of Tools Used",[20,38,39],{},"To perform this operation, we will use two MongoDB tools:",[41,42,43,51],"ul",{},[44,45,46,50],"li",{},[47,48,49],"strong",{},"mongodump",": This tool is used to export the contents of a MongoDB database into a set of BSON files.",[44,52,53,56],{},[47,54,55],{},"mongorestore",": This tool is used to import the contents of a BSON file dump into a MongoDB database.",[20,58,59],{},"Both tools are part of the MongoDB database tools package and can be executed from the command line.",[33,61,63,64,66],{"id":62},"step-1-dump-the-db1-database","Step 1: Dump the ",[24,65,26],{}," Database",[20,68,69,70,72,73,75],{},"The first step in migrating your database is to create a backup of the ",[24,71,26],{}," database. This is done using the ",[24,74,49],{}," command.",[77,78,83],"pre",{"className":79,"code":80,"language":81,"meta":82,"style":82},"language-bash shiki shiki-themes github-light github-dark-default","mongodump --db=db1 --archive=db1.dump\n","bash","",[24,84,85],{"__ignoreMap":82},[86,87,90,93,97],"span",{"class":88,"line":89},"line",1,[86,91,49],{"class":92},"sYRkA",[86,94,96],{"class":95},"sPgVT"," --db=db1",[86,98,99],{"class":95}," --archive=db1.dump\n",[20,101,102],{},"Here’s what each part of this command does:",[41,104,105,114],{},[44,106,107,110,111,113],{},[24,108,109],{},"--db=db1",": This specifies that we are dumping the ",[24,112,26],{}," database.",[44,115,116,119,120,123],{},[24,117,118],{},"--out=\u002Fpath\u002Fto\u002Fdump",": This is the directory where the dump files will be saved. You should replace ",[24,121,122],{},"\u002Fpath\u002Fto\u002Fdump"," with the actual path where you want the backup to be stored.",[20,125,126],{},"Make sure MongoDB is running, and you have the necessary permissions to access and dump the database.",[33,128,130,131,66],{"id":129},"step-2-restore-the-dump-to-the-db2-database","Step 2: Restore the Dump to the ",[24,132,30],{},[20,134,135,136,138,139,141,142,144],{},"Once you have a dump of the ",[24,137,26],{}," database, the next step is to restore this data to the ",[24,140,30],{}," database using ",[24,143,55],{},".",[77,146,148],{"className":79,"code":147,"language":81,"meta":82,"style":82},"mongorestore --nsFrom='db1.*' --nsTo='db2.*' --archive=db1.dump\n",[24,149,150],{"__ignoreMap":82},[86,151,152,154,157,161,164,167],{"class":88,"line":89},[86,153,55],{"class":92},[86,155,156],{"class":95}," --nsFrom=",[86,158,160],{"class":159},"sNbzA","'db1.*'",[86,162,163],{"class":95}," --nsTo=",[86,165,166],{"class":159},"'db2.*'",[86,168,99],{"class":95},[20,170,171],{},"Let’s break down this command:",[41,173,174,185,193],{},[44,175,176,179,180,182,183,113],{},[24,177,178],{},"--nsFrom='db1.*'",": This tells ",[24,181,55],{}," that we want to restore from the ",[24,184,26],{},[44,186,187,190,191,144],{},[24,188,189],{},"--nsTo='db2.*'",": This specifies that the target database for the restore is ",[24,192,30],{},[44,194,195,198,199,144],{},[24,196,197],{},"\u002Fpath\u002Fto\u002Fdump\u002Fdb1",": This is the path to the directory containing the dumped files from ",[24,200,26],{},[20,202,203,204,206,207,209],{},"This command will import all collections from the ",[24,205,26],{}," database into the ",[24,208,30],{}," database, effectively cloning the data.",[33,211,213],{"id":212},"important-considerations","Important Considerations",[41,215,216,240,249],{},[44,217,218,221,222,225,226,229,230,233,234,236,237,239],{},[47,219,220],{},"Authentication",": If your MongoDB instance uses authentication, append ",[24,223,224],{},"--username",", ",[24,227,228],{},"--password",", and ",[24,231,232],{},"--authenticationDatabase"," options to both ",[24,235,49],{}," and ",[24,238,55],{}," commands.",[44,241,242,245,246,248],{},[47,243,244],{},"Data Integrity",": Ensure there are no active writes to the ",[24,247,26],{}," database during the dump to maintain data integrity.",[44,250,251,254],{},[47,252,253],{},"Server Performance",": Both dumping and restoring can be resource-intensive operations. Plan to perform these during off-peak hours if possible.",[33,256,258],{"id":257},"conclusion","Conclusion",[20,260,261],{},"By following these steps, you can easily migrate data from one MongoDB database to another. This method is not only useful for backups but also for replicating data across different environments. Always ensure you test these operations in a development or staging environment before applying them in production to avoid any data loss.",[263,264,265],"style",{},"html pre.shiki code .sYRkA, html code.shiki .sYRkA{--shiki-default:#6F42C1;--shiki-dark:#FFA657}html pre.shiki code .sPgVT, html code.shiki .sPgVT{--shiki-default:#005CC5;--shiki-dark:#79C0FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sNbzA, html code.shiki .sNbzA{--shiki-default:#032F62;--shiki-dark:#A5D6FF}",{"title":82,"searchDepth":267,"depth":267,"links":268},3,[269,271,273,275,276],{"id":35,"depth":270,"text":36},2,{"id":62,"depth":270,"text":272},"Step 1: Dump the db1 Database",{"id":129,"depth":270,"text":274},"Step 2: Restore the Dump to the db2 Database",{"id":212,"depth":270,"text":213},{"id":257,"depth":270,"text":258},"2024-05-08","When working with MongoDB, there are times when you need to migrate data from one database to another. This could be for backup purposes, testing, or moving a production database to a new environment. In this post, I'll guide you through the process of dumping data from a MongoDB database named db1 and restoring it to another database named db2. This is a common task for database administrators and developers alike.","md",{"type":281,"layout":282},"post","blog_page",true,"\u002Fblog\u002F2024-05-08-howto-migrate-a-monogdb",{"title":12,"description":278},"blog\u002F2024-05-08-howto-migrate-a-monogdb",[288,289],"devops","mongodb","bn7CFPp921cPhuEg0FVmc-ZurrLNgDxDCg4VlRLrviU",[292,297],{"title":293,"path":294,"stem":295,"date":296,"children":-1},"Using journalctl","\u002Fblog\u002F2024-02-06-using-journctl","blog\u002F2024-02-06-using-journctl","2024-02-06",{"title":298,"path":299,"stem":300,"date":301,"children":-1},"Setting Up ed25519 SSH Keys and ssh-agent in WSL and Linux","\u002Fblog\u002F2024-05-10-using-ssh-agent","blog\u002F2024-05-10-using-ssh-agent","2024-05-10",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":303},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"m12 19l-7-7l7-7m7 7H5\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":305},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M11.017 2.814a1 1 0 0 1 1.966 0l1.051 5.558a2 2 0 0 0 1.594 1.594l5.558 1.051a1 1 0 0 1 0 1.966l-5.558 1.051a2 2 0 0 0-1.594 1.594l-1.051 5.558a1 1 0 0 1-1.966 0l-1.051-5.558a2 2 0 0 0-1.594-1.594l-5.558-1.051a1 1 0 0 1 0-1.966l5.558-1.051a2 2 0 0 0 1.594-1.594zM20 2v4m2-2h-4\"\u002F>\u003Ccircle cx=\"4\" cy=\"20\" r=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":307},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M19 21v-2a4 4 0 0 0-4-4H9a4 4 0 0 0-4 4v2\"\u002F>\u003Ccircle cx=\"12\" cy=\"7\" r=\"4\"\u002F>\u003C\u002Fg>",1783616164933]