title: how to dynamically set the hostname with MongoRepository
body: <p>I am using Spring-Boot project and MongoRepository instead of MongoTemplate.</p>
  
  <p>When using MongoTemplate, one can dynamically set the hostname by using MongoConnectionPool like so:</p>
  
  <pre><code>@Autowired
  MongoConnectionPool mongoConn
  ....
  mongoConn.setHostname("127.23.45.89");
  mongoConn.setPort(27017);
  </code></pre>
  
  <p>How do I achieve the same effect using MongoRepository?</p>
  
  <p>I know that I can specified the hostname and port by specifying the </p>
  
  <pre><code>spring.data.mongodb.host=hostname1
  spring.data.mongodb.port=27017
  </code></pre>
  
  <p>in application.properties file.  </p>
  
  <p>However I am using GenericContainer to spin up a mongo instance through Docker container to run my unit test.  The container dynamically assigned IP Address and port to the mongo instance and thus I need to be able to dynamically set the hostname and port for the MongoRepository at run time.</p>
  
  <p>This is how I am setting up my unit test.</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = MongoConfiguration.class)
  public class HierarchiesServiceImplTests {
  private static final Logger log = LoggerFactory.getLogger(HierarchiesServiceImplTests.class);
  
  @Autowired
  private HierarchiesService hierarchiesService;
  
  @Autowired
  private HierarchyRepository hierarchyRepo;
  
  @BeforeClass
  public void setUp() {
      MockitoAnnotations.initMocks(this);
  
      startMongo();
  }
  
  /**
   * Starts a Mongo docker container, and configures the repository factory to use this instance.
   */
  private void startMongo() {
      GenericContainer mongo = new GenericContainer("mongo:3")
              .withExposedPorts(27017);
      mongo.start();
  
      String containerIpAddress = mongo.getContainerIpAddress();
      int mappedPort = mongo.getMappedPort(27017);
  
      //TODO: set the hostname and port here so that the MongoRepository use this mongo instance instead of the default localhost.
  
      log.info("Container mongo:3 listening on {}:{}", containerIpAddress, mappedPort);
  }
  </code></pre>
  
  <p>This is what I have in my MongoConfiguration.class</p>
  
  <pre><code>@Configuration
  @EnableMongoRepositories
  @ComponentScan({"com.is.hierarchies.service"})
  public class MongoConfiguration {
  
  }
  </code></pre>