Reputation: 1823
Currently I'm using Amzone EC2 to host my mongo database, below is code for MongoCongig file in java using java mongodb driver and it's working fine.
@Configuration
@EnableMongoRepositories
public class MongoConfig extends AbstractMongoConfiguration
{
@Value("my_amazone_ec2_host")
private String host;
@Value("27017")
private Integer port;
@Value("my_database_name")
private String database;
@Value("database_admin")
private String username;
@Value("admin_pass")
private String password;
@Override
public String getDatabaseName()
{
return database;
}
@Override
@Bean
public Mongo mongo() throws Exception
{
return new MongoClient(
singletonList( new ServerAddress( host, port ) ),
singletonList( MongoCredential.createCredential( username,
database, password.toCharArray() ) ) );
}
}
Now I want to using MongoLab to host my database and MongoLab provide URI to connect to mongo db something like this:
mongodb://<dbuser>:<dbpassword>@ser_num.mongolab.com:port/database_name
I tried to modify my host name with this URI but not successful. Can anyone help me config this file? I'm using only java configuration, not XML configuration; MongoDB version 3.
Upvotes: 0
Views: 339
Reputation: 1823
I just found the solution by replacing relative information from MongoLab URI:
@Value("ser_num.mongolab.com")
private String host;
@Value("port")
private Integer port;
Upvotes: 0