Upgrading MongoDB to a new version and Relocating the Data Directory

Upgrading MongoDB to a new version and Relocating the Data Directory

Overview

This article describes how to safely upgrade MongoDB on Windows (for example, from version 8.0.x to 8.2.x) while moving your data directory out of the version-specific installation folder (C:\Program Files\MongoDB\Server\8.0\data) into a stable path (e.g., C:\data\db). This prevents future upgrades from risking your database files.


Prerequisites

  • Your Current MongoDB installation (in this example, 8.0.x)

  • Administrator rights on Windows

  • Enough disk space to copy/move the data

  • Backup method (optional but strongly recommended)


Step 1. Back Up Your Data

Before any upgrade, make a backup of your database.

Copy data folder (fastest if MongoDB is stopped):

net stop MongoDB xcopy "C:\Program Files\MongoDB\Server\8.0\data" "C:\backup\mongo_data" /E /I

Step 2. Stop the MongoDB Service

Open an Administrator Command Prompt and run:

net stop MongoDB

Step 3. Create New Data and Log Folders

mkdir C:\data\db mkdir C:\data\log

Step 4. Move the Data

Use robocopy to move data out of the versioned installation folder:

robocopy "C:\Program Files\MongoDB\Server\8.0\data" "C:\data\db" /E /COPY:DATSO /DCOPY:DAT /R:0

If logs are under the old folder, move or recreate them under C:\data\log.


Step 5. Update the Configuration File

Locate your config file (usually at C:\Program Files\MongoDB\Server\8.0\bin\mongod.cfg) and edit the following sections:

storage: dbPath: C:\data\db systemLog: destination: file path: C:\data\log\mongod.log logAppend: true

✅ Tip: Consider copying the config to a stable location such as
C:\Program Files\MongoDB\config\mongod.cfg
so it won’t be tied to a specific MongoDB version folder.


Step 6. Install MongoDB New Version (in this example, 8.2)

  1. Download the MSI for MongoDB 8.2.x (Windows x64) from the MongoDB Download Center.

  2. Run the installer. By default, it installs into:

    C:\Program Files\MongoDB\Server\8.2\
  3. Leave the service name as MongoDB.


Step 7. Point the Service to the New Config

Check the service definition:

sc qc MongoDB

If it still points to the old config, update it:

sc config MongoDB binPath= "\"C:\Program Files\MongoDB\Server\8.2\bin\mongod.exe\" --config \"C:\Program Files\MongoDB\config\mongod.cfg\" --service"

Step 8. Start MongoDB

net start MongoDB

Step 9. Verify the Upgrade

In mongosh or MongoDB Compass:

db.version() // should report 8.2.x db.adminCommand({ getCmdLineOpts: 1 }).parsed.storage.dbPath

Make sure the dbPath shows C:\data\db.


Step 10. Enable 8.2 Features (Optional)

Once you confirm stability, update the Feature Compatibility Version (FCV):

db.adminCommand({ setFeatureCompatibilityVersion: "8.2", confirm: true })

Summary

  • Data is now stored in a stable path (C:\data\db) independent of MongoDB versions.

  • Future upgrades only require installing the new binaries and updating the service path.

  • Backups before upgrades are strongly recommended.