I was faced with this challenge when a workload in my homelab was using a very dated MongoDB 3.6 database, and updating it the workload, its now possible to run MongoDB 7.
I was not able to easily find any resources online that described this kind of upgrade so I decided to write my own.
The upgrade path for MongoDB is complex, requiring the sequence:
3.6 → 4.0
4.0 → 4.2
4.2 → 4.4
4.4 → 5.0
5.0 → 6.0
6.0 → 7.0
Additionally the package installation on Debian, particularly for the EOL versions is complex – so I opted to use Docker images from MongoDB for each version. In my case I performed this on a Debian environment under WSL.
At the start and the end I transferred the MongoDB database to and from the folder ~/db
With this process there were some differences with version 6 and 7 – but it was fairly straightforward.
I first began the migration by retrieving the docker images
docker pull mongo:3.6
docker pull mongo:4.0
docker pull mongo:4.2
docker pull mongo:4.4
docker pull mongo:5.0
docker pull mongo:6.0
docker pull mongo:7.0
Version 3.6
I felt it important to validate setFeatureCompatibilityVersion was set to 3.6 and confirm the Docker image is able to read my database
I started the container:
docker run -d \
--name mongo36 \
-v ~/db:/data/db \
-p 27017:27017 \
mongo:3.6
I spawned a mongo shell:
docker exec -it mongo36 mongo
I validated setFeatureCompatibilityVersion:
use admin
db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })
// If needed, set it to 3.6
db.adminCommand({ setFeatureCompatibilityVersion: "3.6" })
// exit the Mongo shell
exit
I stopped the container:
docker stop mongo36
docker rm mongo36
Version 4.0
I started the container:
docker run -d \
--name mongo40 \
-v ~/db:/data/db \
-p 27017:27017 \
mongo:4.0
I spawned a mongo shell:
docker exec -it mongo40 mongo
I set the new setFeatureCompatibilityVersion:
use admin
// Confirm current FCV
db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })
// Now set to 4.0
db.adminCommand({ setFeatureCompatibilityVersion: "4.0" })
exit
I stopped the container:
docker stop mongo40
docker rm mongo40
Version 4.2
I started the container:
docker run -d \
--name mongo42 \
-v ~/db:/data/db \
-p 27017:27017 \
mongo:4.2
I spawned a mongo shell:
docker exec -it mongo42 mongo
I set the new setFeatureCompatibilityVersion:
use admin
// Confirm current FCV
db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })
// Now set to 4.2
db.adminCommand({ setFeatureCompatibilityVersion: "4.2" })
exit
I stopped the container:
docker stop mongo42
docker rm mongo42
Version 4.4
I started the container:
docker run -d \
--name mongo44 \
-v ~/db:/data/db \
-p 27017:27017 \
mongo:4.4
I spawned a mongo shell:
docker exec -it mongo44 mongo
I set the new setFeatureCompatibilityVersion:
use admin
// Confirm current FCV
db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })
// Now set to 4.4
db.adminCommand({ setFeatureCompatibilityVersion: "4.4" })
exit
I stopped the container:
docker stop mongo44
docker rm mongo44
Version 5.0
I started the container:
docker run -d \
--name mongo50 \
-v ~/db:/data/db \
-p 27017:27017 \
mongo:5.0
I spawned a mongo shell:
docker exec -it mongo50 mongo
I set the new setFeatureCompatibilityVersion:
use admin
// Confirm current FCV
db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })
// Now set to 5.0
db.adminCommand({ setFeatureCompatibilityVersion: "5.0" })
exit
I stopped the container:
docker stop mongo50
docker rm mongo50
Version 6.0
I started the container:
docker run -d \
--name mongo60 \
-v ~/db:/data/db \
-p 27017:27017 \
mongo:6.0
I spawned a mongo shell:
docker exec -it mongo60 mongosh
I set the new setFeatureCompatibilityVersion:
use admin
// Confirm current FCV
db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })
// Now set to 6.0
db.adminCommand({ setFeatureCompatibilityVersion: "6.0" })
exit
I stopped the container:
docker stop mongo60
docker rm mongo60
Version 7.0
I started the container:
docker run -d \
--name mongo70 \
-v ~/db:/data/db \
-p 27017:27017 \
mongo:7.0
I spawned a mongo shell:
docker exec -it mongo70 mongosh
I set the new setFeatureCompatibilityVersion:
use admin
// Confirm current FCV
db.adminCommand({ getParameter: 1, featureCompatibilityVersion: 1 })
// Now set to 7.0
db.adminCommand({ setFeatureCompatibilityVersion: "7.0", confirm: true })
exit
I stopped the container:
docker stop mongo70
docker rm mongo70