In this guide, the goal is to update MongoDB Replicas from version 4.4 to 7.0 seamlessly. It is not possible to upgrade directly from version 4.4 to version 7.0. Therefore, a gradual transition must be made between each major version.

This example has been carried out on Ubuntu 22.04.

1) Updating Secondary Nodes from Mongo 4.4 to 5.0

First, we need to update each of the MongoDB secondary nodes one by one to version 5.0

sudo apt-mark unhold mongo*
sudo systemctl stop mongod

# Add MongoDB 5.0 key
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

# Add MongoDB 5.0 repository
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list 
sudo apt update 

# When installing the update, you must keep your current config file by answering the update config file question with N 
sudo apt install -y mongodb-org=5.0.6 mongodb-org-server=5.0.6 mongodb-org-shell=5.0.6 mongodb-org-mongos=5.0.6 mongodb-org-tools=5.0.6
BASH

Very Important !

During the installation, the system will ask whether it should overwrite the config file. In case the configuration file is accidentally overwritten, it will be necessary to revert to the previous settings.

# sudo vi /etc/mongod.conf

# The old file is as follows by default

storage:
  dbPath: /var/lib/mongodb
  wiredTiger:
    engineConfig:
       cacheSizeGB: 2

systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

net:
  port: 25080
  bindIp: 0.0.0.0

replication:
  replSetName: apinizer-replicaset

security:
  authorization: enabled
  keyFile:  /etc/mongodb/keys/mongo-key

setParameter:
  transactionLifetimeLimitSeconds: 300

processManagement:
  timeZoneInfo: /usr/share/zoneinfo

# Start MongoDB service
sudo apt-mark hold mongo*
sudo systemctl start mongod
sudo systemctl status mongod
BASH

Check the status of the replica set by connecting to any node:

mongo mongodb://<NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Checking the status of the replica-set
rs.status()
BASH

2) Reducing the Primary Node to Secondary

The Primary Node is accessed, and its role is demoted to Secondary, prompting the election of a new Primary Node.

# Connect to the Primary Node.
mongo mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Reducing the Primary Node to a Secondary Node.
rs.stepDown()

# After waiting for a while, it is checked whether one of the Secondary Nodes has become the Primary Node.
rs.status()
BASH

3) Updating the Reduced Primary Node from Mongo 4.4 to 5.0

sudo apt-mark unhold mongo*
sudo systemctl stop mongod

# The MongoDB 5.0 key is added, and the repository is updated.
wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

sudo apt update

# When installing the update, you must keep your current config file by answering the update config file question with N 
sudo apt install -y mongodb-org=5.0.6 mongodb-org-server=5.0.6 mongodb-org-shell=5.0.6 mongodb-org-mongos=5.0.6 mongodb-org-tools=5.0.6
BASH
# Start the MongoDB service
sudo apt-mark hold mongo*
sudo systemctl start mongod
sudo systemctl status mongod
BASH

Check the status of the replica set by connecting to any node

mongo mongodb://<NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Checking the status of the replica-set
rs.status()
BASH

4) Mongo 5.0 Feature Compatibility Check

After all nodes have migrated to MongoDB 5.0, the feature compatibility setting is set to 5.0 (on the Primary Node) before migrating to Mongodb 6.

# Connect to primary node
mongosh mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p
use admin  

# If the feature compatibility is 4 as the output of the following command, it should be updated to 5.
db.runCommand({ getParameter: 1, featureCompatibilityVersion: 1 })  

# If the value is not 5.0, you can change it with the following command
db.adminCommand({ setFeatureCompatibilityVersion: "5.0" })
BASH

5) Update Secondary Nodes from Mongo 5.0 to 6.0

Once we are sure that all MongoDB replicas have migrated to version 5.0, we can migrate to version 6.0. First, start by updating all secondary nodes in order:

sudo apt-mark unhold mongo*
sudo systemctl stop mongod

# Add key of the MongoDB 6.0 and update to repository
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt update 

# Install to update
sudo apt install -y mongodb-org=6.0.0 mongodb-org-server=6.0.0 mongodb-org-shell=6.0.0 mongodb-org-mongos=6.0.0 mongodb-org-tools=6.0.0

# Start MongoDB service
sudo apt-mark hold mongo*
sudo systemctl start mongod
sudo systemctl status mongod
BASH

Check the status of the replica set by connecting to any node:

mongosh mongodb://<NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Checking the status of the replica-set
rs.status()
BASH

6) Reducing the Primary Node to Secondary

# Connecting to primary node
mongosh mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Reducing primary node to secondary node
rs.stepDown()

# After waiting for a while, it is checked whether one of the Secondary Nodes has become the Primary Node.
rs.status()
BASH

7) Updating Downgraded Primary Node from Mongo 5.0 to 6.0

sudo apt-mark unhold mongo*
sudo systemctl stop mongod  

# Add key of the MongoDB 6.0 and update to repository
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
sudo apt update 

# Install to update
sudo apt install -y mongodb-org=6.0.0 mongodb-org-server=6.0.0 mongodb-org-shell=6.0.0 mongodb-org-mongos=6.0.0 mongodb-org-tools=6.0.0

# Start MongoDB service
sudo apt-mark hold mongo*
sudo systemctl start mongod
sudo systemctl status mongod
BASH

Check the status of the replica set by connecting to any node:

mongosh mongodb://<NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Check the status of the Replica-Set
rs.status()
BASH

8) Mongo 6.0 Feature Compatibility Check

After all nodes are migrated to MongoDB 6.0, the feature compatibility setting is set to 6.0 (on the Primary Node).

# Connected to primary node.
mongosh mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p
use admin

# If the feature compatibility is 5 as the output of the following command, it should be updated to 6
db.runCommand({ getParameter: 1, featureCompatibilityVersion: 1 })

# If the value is not 6.0, you can change it with the following command  
db.adminCommand( { setFeatureCompatibilityVersion: "6.0" } )
BASH

9) Update Secondary Nodes from Mongo 6.0 to 7.0

Once we are sure that all MongoDB replicas have migrated to version 6.0, we can migrate to version 7.0. First, start by updating all secondary nodes in order:

sudo apt-mark unhold mongo*
sudo systemctl stop mongod

# Add key of the MongoDB 7.0 and update to repository
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt update 

# Install to update
sudo apt install -y mongodb-org=7.0.0 mongodb-org-server=7.0.0 mongodb-org-shell=7.0.0 mongodb-org-mongos=7.0.0 mongodb-org-tools=7.0.0

# Start MongoDB service
sudo apt-mark hold mongo*
sudo systemctl start mongod
sudo systemctl status mongod
BASH

Check the status of the replica set by connecting to any node:

mongosh mongodb://<NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Check the status of the Replica-Set
rs.status()
BASH

10) Reducing the Primary Node to Secondary

# Connecting to primary node
mongosh mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Reducing primary node to secondary node
rs.stepDown()

# After waiting for a while, it is checked whether one of the Secondary Nodes has become the Primary Node.
rs.status()
BASH

11) Updating Downgraded Primary Node from Mongo 6.0 to 7.0

sudo apt-mark unhold mongo*
sudo systemctl stop mongod

# Add key of the MongoDB 7.0 and update to repository
wget -qO - https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
sudo apt update

# Install to update
sudo apt install -y mongodb-org=7.0.0 mongodb-org-server=7.0.0 mongodb-org-shell=7.0.0 mongodb-org-mongos=7.0.0 mongodb-org-tools=7.0.0

# Start MongoDB service
sudo apt-mark hold mongo*
sudo systemctl start mongod
sudo systemctl status mongod
BASH

Check the status of the replica set by connecting to any node:

mongosh mongodb://<NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p

# Check the status of the Replica-Set
rs.status()
BASH

12) Mongo 7.0 Feature Compatibility Check

After all nodes are migrated to MongoDB 7.0, the feature compatibility setting is set to 7.0 (on the Primary Node).

# Connected to primary node.
mongosh mongodb://<PRIMARY_NODE_IP>:25080 --authenticationDatabase "admin" -u "apinizer" -p
use admin

# If the feature compatibility is 6 as the output of the following command, it should be updated to 7
db.runCommand({ getParameter: 1, featureCompatibilityVersion: 1 })

# If the value is not 7.0, you can change it with the following command  
db.adminCommand( { setFeatureCompatibilityVersion: "7.0", confirm: true } )
BASH