This is the primary program of node.js to connect MongoDB. Copied from the internet because my actual code not working, so I am trying this one. This raises the same issue.
Problem with this code or in my local machine. When I ran the program, it got stuck or in any loop, and after a few minutes, it shows timeout, without any further detailed error message.
const MongoClient = require("mongodb").MongoClient;
// Connection URL
const url = "mongodb://localhost:27017";
// Database Name
const dbName = "myproject";
// Create a new MongoClient
const client = new MongoClient(url);
// Use connect method to connect to the Server
client.connect(function (err) {
console.log("Connected successfully to server");
const db = client.db(dbName);
client.close();
});
Try to connect with atlas or local mongoDB compass.
2
Answers
Please try the below code for the DB connection.
Check this code
replace with your username and password