Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
Specifying a port in MongoDB URI does not allow me to connect to my local DB

I am testing a connection to a local DB using mongoose and mongodb. Whenever I specify a port when passing in the URI to mongoose.connect() I get a connection refused error,

async function connectDB() {
    const db = await mongoose.connect('mongodb://localhost:<PORT NR>/myCollection')
    return db
}

However, the connection works whenever I do not specify a port number.

async function connectDB() {
    const db = await mongoose.connect('mongodb://localhost/myCollection')
    return db
}

Why is this the case? I have used the port numbers: 3000,3232,27017 and 3456. All of which to my knowledge are not in use.

over 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you are running mongo in default port then you can try to connect in below way

function connectMongoDB() {
    let uri = "mongodb://localhost:27017/<db_name>?authSource=admin&ssl=false";
    mongoose.connect(uri, { useUnifiedTopology: true, useNewUrlParser: true });

    const connection = mongoose.connection;

    connection.once("open", function() {
        console.log("MongoDB database connection established successfully");
    }); 
}
over 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Virtual assistant