Upgrade MongoDB 3 to 4 on Ubuntu

by Christian Fei @ 2019-07-16

220 words, 1 minute reading time

#post #tutorial #mongodb #featured 

Do you need to upgrade from MongoDB version 3.4 / 3.6 to version 4.0 ?

Starting from 3.4

If you are upgrading from version 3.4, you'll need to upgrade first to 3.6 and then to 4.0.

Read here for more info.

To do so, enter the mongo shell and verify the current compatibility setting:

db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )

Afterwards, set the feature compatibility version to 3.4 like so:

db.adminCommand( { setFeatureCompatibilityVersion: "3.4" } )

Upgrading to 3.6

First, remove any source file related to 3.4 (the file should be mongodb-org-3.4.list):

ls -lha /etc/apt/sources.list.d/
# you should see mongodb-org-3.4.list, remove it

Get the latest keys and update the repository:

sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

apt-get update
apt-get remove mongodb-org
apt-get install -y mongodb-org

Starting from 3.6

Once you're on 3.6, you can safely upgrade to 4.0, by setting the feature compatibility version first.

Enter the mongo shell and execute:

db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )

Upgrading to 4.0

First, remove any source file related to 3.6 (the file should be mongodb-org-3.6.list):

ls -lha /etc/apt/sources.list.d/
# you should see mongodb-org-3.6.list, remove it

Get the latest keys and update the repository:

sudo apt-key adv -keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

apt-get update
apt-get remove mongodb-org
apt-get install -y mongodb-org

Next steps

Check the logs to verify everything is ok: /var/log/mongodb/mongodb.log

FEEDBACK @christian_fei

Featured blog posts

Twitter oauth by example in node.js So long, and thanks for all the veggies Simplest vanilla javascript static site blog search for jekyll, hugo, 11.ty Minimal environments with dotenv and node.js Connect to mongodb with monk in node.js Lazy loading images in 2020 Recover from failed `lerna` publish The cleanest way to maintain connect / express middlewares in node.js Better web scraping with node.js My blogging stack in 2020