title: MongoDB specify database in connection string
body: <p>There are loads of SO's out there, but there one plain thing I don't understand, hence the creation of this SO.</p>
  
  <p>Take the following connection string <code>mongodb//admin:1234@10.0.125.1/my_database</code>. With this connection string, I would expect that I was able to connect to the MongoDB instance, and the specific database <code>my_database</code>.</p>
  
  <p>Following several SO's and othe rarticles, this should work, but if I look at the official documentation from <a href="https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html" rel="noreferrer">MongoDB</a>, the <code>database</code> option is the database to which I want to authenticate.</p>
  
  <blockquote>
    <p>/database is the name of the database to login to and thus is only relevant if the username:password@ syntax is used. If not specified the “admin” database will be used by default.</p>
  </blockquote>
  
  <p>I want my user to be authenticated towards the admin database (as that's where my user lies), but I want to access the database <code>my_database</code>. I'd expect this procedure to work:</p>
  
  <pre><code>private static string _connectionString = ConfigurationManager.ConnectionStrings["MongoDB"].ToString();
  
  public static IMongoDatabase GetDatabase()
  {
      var _url = MongoUrl.Create(_connectionString);
      var _databaseName = _url.DatabaseName;
      return new MongoClient(_connectionString).GetDatabase(_databaseName);
  }
  </code></pre>
  
  <p>But whenever I do this, I'm receiving a timeout for any calls made to the MongoDB. An example is this:</p>
  
  <pre><code>public List&lt;SomeObject&gt; GetAllObjects()
  {
      var database = DatabaseInstance.GetDatabase();
  
      // Error is thrown here (timout after 30000ms)
      var objects = database.Getcollection&lt;SomeObject&gt;("SomeObjects").Find(Builders&lt;SomeObject&gt;.Filter.Empty).Find();
      return objects;
  }
  </code></pre>
  
  <p>Now, if I were to remove the database from the connection string and hard code the database name in my <code>return new MongoClient(_connectionString).GetDatabase("my_database");</code>, everything works as expected.</p>
  
  <p>This is where I don''t understand the <strong>/database</strong> option in the connection string. I'd really appreciate it if someone could shed some light on this.</p>