mongodb nodejs connect localhost

// Step 1: Require the 'mongodb' package
const { MongoClient } = require('mongodb');

// Step 2: Define the connection URL with the appropriate database name
const url = 'mongodb://localhost:27017/your-database-name';

// Step 3: Create a MongoClient instance and connect to the MongoDB server
const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });

// Step 4: Connect to the MongoDB server
client.connect()
  .then(() => {
    // Step 5: Access the MongoDB database through the client
    const database = client.db();

    // Step 6: Perform database operations (e.g., insert, update, find)

    // Step 7: Close the connection when done
    client.close();
  })
  .catch(err => console.error(err));