If you have been using the UniFi controller for a very long time, there’s a chance you are running an older version of MongoDB. When Ubiquiti released version 8.1 of the UniFi network application server, they finally bumped up the supported MongoDB version from 4.4 to 7.0.
The MongoDB upgrade path only supports jumping one major version at a time, and some manual steps are needed. Yes, you could take a settings-only backup and reroll the whole setup, but where’s the fun in that?
In this post, I will show you step-by-step how to upgrade the Mongo database version from 4.4 to 7.0 for the Unifi network application server running on docker.
Prerequisites
- Backup your UniFi config. My blog post, UniFi Network Server Settings Backup and Export, covers how to do this.
- Running UniFi network application version 8.1 or newer.
- The service name of the MongoDB for UniFi from your docker compose file.
- The container name of your MongoDB for UniFi.
The Process
The entire process revolves around docker exec as we will execute commands directly into the Mongo database container.
You can follow this process on other UniFi controller setups that aren’t done with docker. However, I’ve only validated the process with on a docker setup, but the command structure would be similar.
To be extra safe during this process, I will bring down my UniFi docker stack and only bring up my UniFi database container.
I will run the command docker compose down
to bring the UniFi controller offline. This helps ensure the UniFi network application doesn’t try to write to the database while the database is upgrading.
- Bring up the MonogoDB container for UniFi with the following command. Replace Mongo_Service_Name with the service name for MongoDB from your docker compose file.
sudo docker compose up Mongo_Service_Name -d
If you are using my docker compose setup from my blog post, UniFi Network Server with Docker, the MongoDB service name will be unifi-net-app-db
For me, the command will be sudo docker compose up unifi-net-app-db -d
Before starting any upgrades, let’s double-check which version of MongoDB is installed and what the compatibility level is currently set to. We will do that by using the MongoDB shell with the command mongo
. We will tell Mongo that we will provide some commands from which we want the output by using the argument eval
followed by our commands.
The command to check which version of MongoDB we are running and what our compatibility level is set to is the following command db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
To run this against our Mongo docker container, we will use docker exec with a shell command followed by the mongo shell commands we want to run.
- Run the following command to check what MongoDB version is running. Replace Mongo_Container_Name with the name of the MongoDB container that you are using with UniFi.
sudo docker exec Mongo_Container_Name sh -c 'mongo --eval "db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )"'
For me, the command looks like
sudo docker exec unifi-net_DB sh -c 'mongo --eval "db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )"'
Your output should be something like this
MongoDB server version: 4.4.29
{ "featureCompatibilityVersion" : { "version" : "4.4" }, "ok" : 1 }
The output above confirms that we are running MongoDB 4.4, and our feature compatibility is set to the same version.
If you get an error that mongo is not found, this is due to running a newer version of MongoDB that no longer supports the mongo command as that command has been deprecated in newer versions.
The replacement command for mongo
is mongosh
. However, the output differs from the old mongo command when gathering the compatibility levels.