xxxxxxxxxx
// Import the required libraries
const { MongoClient } = require('mongodb');
// Connection URI
const uri = 'mongodb://localhost:27017';
// Create a new MongoClient instance
const client = new MongoClient(uri, { useUnifiedTopology: true });
// Connect to the MongoDB server
client.connect((err) => {
if (err) {
console.error('Failed to connect to MongoDB:', err);
return;
}
console.log('Connected to MongoDB successfully!');
// Perform further operations with the connected database
const db = client.db('mydatabase');
// ... Your code here ...
// Close the connection
client.close();
});