title: MongoDB/Mongoose Connection Error when Dbname is used in the connection string
body: <p>A NodeJS app uses mongoose 5.6.0 to connect to MongoDB 4.0.10 which runs on localhost inside a docker container.</p>
  
  <p>The connection can be established when we use </p>
  
  <pre><code>const mongoUri = 'mongodb://admin:mypassword@127.0.0.1:27017'
  mongoose.connect(mongoUri)
  </code></pre>
  
  <p><strong>Problem:</strong> We start getting authentication errors when we include the name of the database we are connecting to. There is no problem using Python to connect to the same MongoDB database.</p>
  
  <pre><code>const mongoUri = 'mongodb://admin:mypassword@127.0.0.1:27017/my-db'
  mongoose.connect(mongoUri)
  </code></pre>
  
  <p>and also tried</p>
  
  <pre><code>const mongoUri = 'mongodb://admin:mypassword@127.0.0.1:27017/my-db'
  mongoose.connect(mongoUri, { useNewUrlParser: true })
  </code></pre>
  
  <blockquote>
    <blockquote>
      <p>UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: Authentication failed.</p>
    </blockquote>
  </blockquote>
  
  <p>Why is it unable to make the connection and how can we solve this problem?</p>
  
  <h2>Update</h2>
  
  <p>Found the solution to be</p>
  
  <pre><code>const mongoUri = 'mongodb://admin:mypassword@127.0.0.1:27017'
  mongoose.connect(mongoUri, { useNewUrlParser: true, dbName: 'my-db' })
  </code></pre>
  
  <p>Why must the <code>dbname</code> be passed as an option instead of including it in the connection string?</p>