how to use mongodb with node js code example
Example 1: how to install mongodb in node js
npm install mongodb --save
Example 2: 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 3: how to connect mongodb and nodejs
const {MongoClient} = require('mongodb');