Difference between MongoFactoryBean and SimpleMongoDbFactory Difference between MongoFactoryBean and SimpleMongoDbFactory mongodb mongodb

Difference between MongoFactoryBean and SimpleMongoDbFactory


I am surprised this question has still not been answered. And though this might not be the exact answer you are looking for, here is my take on things. I have found that using the second approach, MongoFactoryBean, is the best approach.

Simply because there are more configuration options. For instance, if you want to set an Exception Translator, you can do so easily with MongoFactoryBean.

If I remember correctly, and I may be wrong, the MongoFactoryBean is meant for convenience for MongoDbFactory. Meaning, it adds another layer of abstraction.

All in all, go with the second approach.


I believe that in the first option you can also set the same settings of the second option:

public @Bean MongoDbFactory mongoDbFactory() throws Exception {    UserCredentials credentials = new UserCredentials(env.getProperty("db.username"), env.getProperty("db.password"));    return new SimpleMongoDbFactory(new MongoClient(env.getProperty("db.host"), env.getProperty("db.port",Integer.class, 27017)), env.getProperty("db.name"), credentials);}public @Bean MongoTemplate mongoTemplate() throws Exception {    return new MongoTemplate(mongoDbFactory());}