Lesson 2: MongoDB Client Driver Upgrades / Learn
Code Summary: MongoDB Client Driver Upgrades
Review the following code, which demonstrates how to upgrade a MongoDB driver. This example uses Node.js, but the process is similar with other drivers.
Check the Version of Node.js
The following code shows how to check the version of Node.js:
node -v
Confirm the MongoDB Node.js Driver Version
The following code shows how to check the package.json file for the MongoDB Node.js driver:
grep mongodb package.json
Check Compatibility
To determine the compatibility between MongoDB and your language driver, check the MongoDB documentation for your language driver.
Upgrade Node Versions
The following code upgrades Node.js to version 16 by using node version manager (nvm):
nvm install v16
Running node -v confirms that the Node.js version is 16:
node -v
Upgrade the MongoDB Driver
The following code shows how to upgrade the Node.js MongoDB driver to 5.0 by running npm i mongodb:
npm i mongodb
View the Node.js Version 12 Application
Here’s the code for the Node.js version 12 application:
const { MongoClient } = require('mongodb');
const uri = "";
const client = new MongoClient(uri);
client.connect((err) => {
const collection = client.db("sample_supplies").collection("sales");
console.log("err");
collection.findOne({}).then((sale) => {
console.log(sale);
});
client.close();
});
View the Node.js Version 16 Application
Here’s the code for the Node.js version 16 application with async/await:
const { MongoClient, ServerApiVersion } = require('mongodb');
const uri = "";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true, serverApi: ServerApiVersion.v1 });
async function run() {
try {
const collection = client.db("sample_supplies").collection("sales");
const query = await collection.findOne({});
console.log(query);
} finally {
await client.close();
}
}
run().catch(console.dir);