EzyMongo: Configuration

Updated at 1685687008000

1. Configuration List

EzyMongo provides a list of configurations in bellow table:

PropertyDefault ValueDescription
1database.mongo.urinullMongoDB URI
2database.mongo.hostnullMongoDB host
3database.mongo.port0MongoDB port
4database.mongo.usernamenullMongoDB username
5database.mongo.passwordnullMongoDB password
6database.mongo.databasenullMongoDB database name
7database.mongo.collection.naming.caseNATURECollection name style
8database.mongo.collection.naming.ignored_suffixnullSuffix to remove from collection's name

You can encrypt sensitive configuration like username, password if you want.

2. An Example

You can put bellow configuration to your application.yaml file:

    # for application.yaml
    database:
      mongo:
        uri: mongodb://ezydata_mongo:ezydata_mongo@localhost:27017/ezydata-mongo
        database: ezydata-mongo
        collection:
          naming:
            case: UNDERSCORE
            ignored_suffix: Entity

Or for application.properties file:

    # for application.properties
    database.mongo.uri=mongodb://ezydata_mongo:ezydata_mongo@localhost:27017/ezydata-mongo
    database.mongo.database=ezydata-mongo
    database.mongo.collection.naming.case=UNDERSCORE
    database.mongo.collection.naming.ignored_suffix=Entity

3. Auto Configuration

To configure automatically, you just need add configuration and add ezyfox-boot-autoconfigure to your project dependency like this:

<dependency>
    <groupId>com.tvd12</groupId>
    <artifactId>ezyfox-boot-autoconfigure</artifactId>
    <version>1.1.0</version>
</dependency>

4. Configure Manually

For some reason, you can not use ezyfox-boot-autoconfigure you can config like this:

    import com.mongodb.MongoClient;
    import com.tvd12.ezydata.database.EzyDatabaseContext;
    import com.tvd12.ezydata.mongodb.EzyMongoDatabaseContextBuilder;
    import com.tvd12.ezydata.mongodb.loader.EzySimpleMongoClientLoader;
    import com.tvd12.ezyfox.annotation.EzyProperty;
    import com.tvd12.ezyfox.bean.EzyBeanConfig;
    import com.tvd12.ezyfox.bean.EzyPackagesToScanAware;
    import com.tvd12.ezyfox.bean.EzySingletonFactory;
    import com.tvd12.ezyfox.bean.EzySingletonFactoryAware;
    import com.tvd12.ezyfox.bean.annotation.EzyConfigurationBefore;
    import com.tvd12.ezyfox.util.EzyPropertiesAware;
    import lombok.Setter;
    import java.util.Map;
    import java.util.Properties;
    @Setter
    @EzyConfigurationBefore
    public class EzyMongoConfiguration implements
            EzyBeanConfig,
            EzyPropertiesAware,
            EzySingletonFactoryAware {
        @EzyProperty("database.mongo.database")
        private String databaseName;
        private Properties properties;
        private EzySingletonFactory singletonFactory;
        @Override
        public void config() {
            EzyDatabaseContext databaseContext = newMongodbDatabaseContext();
            Map repos = databaseContext.getRepositoriesByName();
            for (String repoName : repos.keySet()) {
                singletonFactory.addSingleton(repoName, repos.get(repoName));
            }
        }
        private EzyDatabaseContext newMongodbDatabaseContext() {
            EzyMongoDatabaseContextBuilder builder = new EzyMongoDatabaseContextBuilder()
                    .properties(properties)
                    .scan("your_package_to_scan")
                    .mongoClient(newMongoClient())
                    .databaseName(databaseName);
            return builder.build();
        }
        protected MongoClient newMongoClient() {
            return EzySimpleMongoClientLoader.load(properties);
        }
    }

Next

You can take a look a list of default functions.