Video Transcript (English)
Applications leverage MongoDB by using a language-specific driver. This process is the same whether you are using Atlas or a self-managed deployment. In this video, you’ll learn about compatibility when upgrading drivers. We’ll also discuss how upgrading can affect performance and how the Stable API works.
For the examples in this video, we’ll use Node.js, but the examples can be applied to most other languages as well.
Before upgrading anything MongoDB related, it’s crucial to check the compatibility tables in the documentation for your driver.
Let’s go through an example of an upgrade process in MongoDB.
In our Linux terminal, we have a Node.js application that we want to update to the latest version of MongoDB before upgrading our server to 6.0.
But before we do that, we’ll collect some version information. First, we run node –version, which shows that we have Node.js version 12.
Now let’s check our version of the MongoDB driver in the application package.json file. This shows that we’re using version 3.1.
The next step is to look at the driver compatibility tables for Node.js. You can find these in the MongoDB documentation for your language driver.
On this page, there are two tables. The first table specifies the versions of the MongoDB Node.js driver that are recommended for use with MongoDB. The table contains the Node.js driver versions as rows, and the MongoDB versions as columns.
This table indicates that we need to upgrade our driver to 5.0 to get the full benefits of MongoDB 6.0.
The second table specifies the versions of the MongoDB Node.js driver that are recommended for use with a specific version of Node.js. The table contains Node.js driver versions as rows, and the Node.js versions as columns.
It looks like I also need to update my version of Node.js to work with the Node.js 5.0 driver.
We can also check the “What’s new” tab to get a better understanding of changes across versions and if any of our code will become deprecated or obsolete after upgrading.
Another thing to consider before upgrading is performance changes. Sometimes upgrading can speed up or slow down performance.
After upgrading, you should always thoroughly test your application in a pre-production environment before moving it to production.
Back in the terminal, we’ll upgrade our version of Node.js to Node.js version 16 using node version manager.
Then we run node -v to confirm the Node.js version is 16.
Next, we’ll upgrade our Node.js MongoDB driver to 5.0 in our application by running npm i mongodb.
Let’s check the package.json.
Here’s our current application. It’s a simple Node.js client that connects to a MongoDB server and performs a find query.
Let’s test it with our newly upgraded driver.
Hmm, it appears that it no longer works as intended.
This is because the updated driver wants to use async/await instead of callbacks.
Let’s update the code to use async/await.
We wrap everything in a main function and change the promise to use await.
Before we exit, notice the serverAPI option that we passed to the client.
The serverAPI option enables the Stable API feature. When you use the Stable API feature with an official MongoDB driver, you can upgrade your MongoDB server at will, and ensure that behavior changes between MongoDB versions won’t break your application.
Now we’ll save and test the application again to see if it works.
Nice! The application works as intended.
In this video, you learned how to read the compatibility tables in the MongoDB driver documentation.
We also explored how upgrading can impact performance of your application.
Finally, you learned that the Stable API allows you to upgrade your MongoDB server at will, and ensure that behavior changes between MongoDB versions won’t break your application.