mongodb connection code in node.js code example
Example 1: how to connect mongodb with node js
async function main(){
const uri = "mongodb+srv://<username>:<password>@<your-cluster-url>/test?retryWrites=true&w=majority";
const client = new MongoClient(uri);
try {
await client.connect();
await listDatabases(client);
} catch (e) {
console.error(e);
} finally {
await client.close();
}
}
main().catch(console.error);
Example 2: how to connect mongodb and nodejs
const {MongoClient} = require('mongodb');