How to use Mongolab's mongodb with Jhipster

Jhipster has many subgenerators to deploy applications to various cloud providers such as Heroku, CloudFoundry, OpenShift etc. However, if the apps use mongodb they cannot be deployed to cloud providers as mongeez bundled with Jhipster requires admin privileges as discussed here

After searching for sometime i found out a workaround here but it was too confusing to follow and it did not work for me.

I stumbled upon mongobee and decided to give it a try.

The following are the steps to replace mongeez with mongobee in jhipster and i verified this works with mongolab.

  • Step 1: Add mongobee dependency to pom.xml

  • Step 2: Edit to instantiate mongobee instead of mongeez

//    @Bean
//    public Mongeez mongeez() {
//        log.debug("Configuring Mongeez");
//        Mongeez mongeez = new Mongeez();
//        mongeez.setFile(new ClassPathResource("/config/mongeez/master.xml"));
//        mongeez.setMongo(mongo);
//        mongeez.setDbName(propertyResolver.getProperty("databaseName"));
//        mongeez.process();
//        return mongeez;
//    }

      public Mongobee mongobee(){

         Mongobee runner = 
             new Mongobee("mongodb://");  
             "com.application.config.DatabaseConfiguration"); // package to scan for changesets
         return runner;
  • Step 3: Create Java classes for mongeez xml config files.

    Mongobee lets you create java config files instead of xml. Create java config files for users.xml and authorities.xml.

@ChangeLog(order = "001")
public class MyMongoChangeSet {

   @ChangeSet(order = "001", id = "ChangeSet-1", author = "jhipster")
   public void someChange(DB db) {
      DBCollection mycollection = db.getCollection("T_AUTHORITY");
      BasicDBObject doc1 = new BasicDBObject().append("_id", "ROLE_ADMIN");
      BasicDBObject doc2 = new BasicDBObject().append("_id", "ROLE_USER");
      mycollection .insert(doc1);
      mycollection .insert(doc2);

Thats's pretty much it.

There's just one small caveat: I see some exceptions thrown at WARN level during application startup which seems to be benign at the moment.

[WARN] org.reflections.Reflections - could not create Vfs.Dir from url. ignoring the exception and continuing
org.reflections.ReflectionsException: could not create Vfs.Dir from url, no matching UrlType was found [file:/System/Library/Java/Extensions/libAppleScriptEngine.jnilib]
either use fromURL(final URL url, final List<UrlType> urlTypes) or use the static setDefaultURLTypes(final List<UrlType> urlTypes) or addDefaultURLTypes(UrlType urlType) with your specialized UrlType.
    at org.reflections.vfs.Vfs.fromURL( ~[reflections-0.9.9.jar:na]
    at org.reflections.vfs.Vfs.fromURL( ~[reflections-0.9.9.jar:na]
    at org.reflections.Reflections.scan( [reflections-0.9.9.jar:na]
    at org.reflections.Reflections.scan( [reflections-0.9.9.jar:na]
    at org.reflections.Reflections.<init>( [reflections-0.9.9.jar:na]
    at org.reflections.Reflections.<init>( [reflections-0.9.9.jar:na]
    at org.reflections.Reflections.<init>( [reflections-0.9.9.jar:na]
    at com.github.mongobee.utils.ChangeService.fetchChangeLogs( [mongobee-0.10.jar:na]
    at com.github.mongobee.Mongobee.execute( [mongobee-0.10.jar:na]
    at com.github.mongobee.Mongobee.afterPropertiesSet( [mongobee-0.10.jar:na]
    at [spring-beans-4.0.7.RELEASE.jar:4.0.7.RELEASE]