Example MongoDB Data Connection

This example configuration shows data connections to two MongoDB databases.

As in the example, you can supply authentication credentials to a MongoDB instance as part of the connection string, or separately.

  • XML

  • YAML

  • Java

  • SQL

<hazelcast>
  <data-connection name="my-mongodb">
    <type>Mongo</type>
    <properties>
      <property name="connectionString">mongodb://my_user:my_password@some-host:27017</property> (1)
      <property name="database">my_database</property> (2)
    </properties>
    <shared>true</shared>
  </data-connection>
  <data-connection name="my-other-mongodb">
    <type>Mongo</type>
    <properties>
      <property name="host">some_host</property> (3)
      <property name="username">my_user</property> (4)
      <property name="password">my_password</property>
      <property name="database">my_other_database</property> (2)
    </properties>
    <shared>true</shared>
  </data-connection>
</hazelcast>
1 (Required) Connection string of the MongoDB instance, including user credentials
2 (Optional) Name of the database to connect to
3 (Optional) Host details of the MongoDB instance, excluding user credentials
4 (Optional) User credentials for the MongoDB instance
hazelcast:
  data-connection:
    my-mongodb:
      type: Mongo
      properties:
        connectionString: mongodb://my_user:my_password@some-host:27017 (1)
        database: my_database (2)
      shared: true
    my-other-mongodb:
      type: Mongo
      properties:
        host: some_host (3)
        username: my_user (4)
        password: my_password
        database: my_other_database (2)
      shared: true
1 (Required) Connection string of the MongoDB instance, including user credentials
2 (Optional) Name of the database to connect to
3 (Optional) Host details of the MongoDB instance, excluding user credentials
4 (Optional) User credentials for the MongoDB instance
config
  .addDataConnectionConfig(
    new DataConnectionConfig("my-mongodb")
      .setType("Mongo")
      .setProperty("connectionString", "mongodb://my_user:my_password@some-host:27017") (1)
      .setProperty("database", "my_database") (2)
      .setShared(true)
  )
  .addDataConnectionConfig(
    new DataConnectionConfig("my-other-mongo")
      .setType("Mongo")
      .setProperty("host", "some-host") (3)
      .setProperty("username", "my_user") (4)
      .setProperty("password", "my_password")
      .setProperty("database", "my_other_database") (2)
      .setShared(true)
  );
1 (Required) Connection string of the MongoDB instance, including user credentials
2 (Optional) Name of the database to connect to
3 (Required) Host details of the MongoDB instance, excluding user credentials
4 (Optional) User credentials for the MongoDB instance

Data connections created in SQL behave differently from those defined in members' configuration files or in Java.

  • To retain SQL-defined data connections after a cluster restart, you must enable SQL metadata persistence. This feature is available in the Enterprise Edition.

  • You can create or drop a data connection using SQL commands. To update a data connection, you need to drop and then recreate it.

CREATE DATA CONNECTION my_mongodb
TYPE Mongo
SHARED
OPTIONS (
    'connectionString'='mongodb://my_user:my_password@some-host:27017', (1)
    'database'='my_database'); (2)
1 (Required) Connection string of the MongoDB instance, including user credentials
2 (Optional) Name of the database to connect to
CREATE DATA CONNECTION my_mongodb
TYPE Mongo
SHARED
OPTIONS (
    'host'='some-host', (1)
    'username'='my_user', (2)
    'password'='my_password'
    'database'='my_other_database');
1 (Required) Host details of the MongoDB instance, excluding user credentials
2 (Optional) User credentials for the MongoDB instance
Property Default value Description When to use

connectionString

The connection string used to connect to given MongoDB instance. More in Mongo documentation.

Can be omitted if you prefer to provide host,

databaseName

Name of the database that can be accessed via this connection. If omitted, user will have access to all databases available to given Mongo user.

If you want to restrict usage of this Data Connection to a particular database. Mandatory if you want to use this Data Connection with GenericMapStore.

username

Username used to authenticate to Mongo.

If you want to avoid putting credentials in connection string, you can use this dedicated option.

password

Password used to authenticate to Mongo.

If you want to avoid putting credentials in connection string, you can use this dedicated option.

authDb

admin

Authentication database - the database that holds user’s data. It is not the same as the databaseName option, both options can specify other databases - one to authenticate, other to actually read.

If you want to use username and password options, the authDb option is the way to configure authentication database name (which can be contained in connectionString).

host

Host to which Hazelcast will connect. Exclusive with connectionString.

If you want to use username and password options, the host option is the way to configure host name.

connectionPoolMinSize

10

Sets the minimum size of MongoClient’s internal connection pool.

If you want to control connection pool size

connectionPoolMaxSize

10

Sets the maximum size of MongoClient’s internal connection pool.

If you want to control connection pool size

enableSsl

Enables SSL support for Mongo client. Default value is false.

invalidHostNameAllowed

Allows invalid hostnames in SSL. Default value is false. See Mongo docs for more info.

keyStore

Location of the key store, file must be present on all members.

keyStoreType

Type of the used key store. Defaults to system default.

keyStorePassword

Password to the key store.

trustStore

Location of the trust store, file must be present on all members.

trustStoreType

Type of the used trust store. Defaults to system default.

trustStorePassword

Password to the trust store.