xxxxxxxxxx
const { MongoClient } = require('mongodb');
async function connectToDatabase() {
const uri = 'mongodb://localhost:27017'; // MongoDB connection string for localhost
const client = new MongoClient(uri);
try {
// Connect to the MongoDB server
await client.connect();
console.log('Connected to the database.');
// Do your database operations here...
} catch (error) {
console.error('Unable to connect to the database:', error);
} finally {
// Close the connection once you're done
await client.close();
console.log('Connection closed.');
}
}
// Call the function to establish the connection
connectToDatabase();
xxxxxxxxxx
To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.