How To Move Existing & Store Future Local MongoDB Data From One Drive To Another (Hard) Drive - Windows 10

How To Move Existing & Store Future Local MongoDB Data From One Drive To Another (Hard) Drive - Windows 10

Subscribe to my newsletter and never miss my upcoming articles

Hello Fellow Codenewbies 👋

It was all started with the lack of space for C: drive on my computer. Finally, to make more space in the C: drive, I decided to move all projects and personal folders to the D: drive.

And then the time came when I wanted to continue one of my NodeJS with MongoDB project.

When I run mongod, it threw me shutting down with code: 100 error as below.

mongod shutting down with code 100

I faced chains of errors afterward.

I've Googled only to find plenty of results to fix the errors with either Linux or macOS and barely found anything for Windows 10.

I had two choices:

  1. Uninstall and re-install MongoDB with a new database path
  2. Go through a series of trials and errors to adapt the steps that need to be done with Linux or macOS to Windows 10

As painful as it is, I've chosen the second way so I could help others who want to do the same.

Without further ado, below is how I did it.


What I Want To Achieve

  • Move existing MongoDB databases from C: to D: drive
  • Store future databases in D: drive

How I Achieved It

  1. Make sure that mongod doesn't run in the background. If it still runs, you can close it with ctrl + c.
  2. Create a folder named data with subfolder db in D: drive (or any other location that you prefer)
  3. Run this command in Windows PowerShell (Admin)

      mongod --dbpath="D:\data\db"
    

    Change the path to where you created your new data folder and make sure that you put the path inside the quote.

  4. Exit mongod with ctrl + c
  5. Copy/cut & paste existing data from data folder in drive C: to D:
  6. Make sure that the mongod.lock file is empty. Otherwise, you can delete it. This won't damage anything and will be automatically regenerated when you run mongod. (When it's not empty, it will throw an error & mongod will automatically shut down.)

✔ Your existing data now should have been moved and you can start to get your data stored in the new directory when you work with one.


Note:
This post is one of my TIL notes based on my own research and experience.

 
Share this