title: Spring-data-mongodb connect to multiple databases in one Mongo instance
body: <p>I am using the latest spring-data-mongodb (1.1.0.M2) and the latest Mongo Driver (2.9.0-RC1). I have a situation where I have multiple clients connecting to my application and I want to give each one their own "schema/database" in the same Mongo server. This is not a very difficult task to achieve if I was using the driver directly:</p>
  
  <pre><code>Mongo mongo = new Mongo( new DBAddress( "localhost", 127017 ) );
  
  DB client1DB = mongo.getDB( "client1" );
  DBCollection client1TTestCollection = client1DB.getCollection( "test" );
  long client1TestCollectionCount = client1TTestCollection.count();
  
  DB client2DB = mongo.getDB( "client2" );
  DBCollection client2TTestCollection = client2DB.getCollection( "test" );
  long client2TestCollectionCount = client2TTestCollection.count();
  </code></pre>
  
  <p>See, easy. But spring-data-mongodb does not allow an easy way to use multiple databases. The preferred way of setting up a connection to <code>Mongo</code> is to extend the <a href="http://static.springsource.org/spring-data/data-mongodb/docs/1.1.0.M2/api/org/springframework/data/mongodb/config/AbstractMongoConfiguration.html" rel="noreferrer">AbstractMongoConfiguration</a> class: </p>
  
  <p>You will see that you override the following method:</p>
  
  <pre><code>getDatabaseName()
  </code></pre>
  
  <p>So it forces you to use one database name. The repository interfaces that you then build use that database name inside the MongoTemplate that is passed into the <code>SimpleMongoRepository</code> class.</p>
  
  <p>Where on earth would I stick multiple database names? I have to make multiple database names, multiple <code>MongoTempate</code>s (one per database name), and multiple other config classes. And that still doesn't get my repository interfaces to use the correct template. If anyone has tried such a thing let me know. If I figure it out I will post the answer here.</p>
  
  <p>Thanks.</p>