Connecting to a distributed set of MongoDB instances

Accessing data that is stored in a distributed set of MongoDB instances is nearly the same procedure as connecting to a single MongoDB instance.

Before you begin

Enable your application to use MongoDB. See Creating Liberty applications that use MongoDB.

Stabilized feature: The mongodb-2.0 feature is stabilized. The MongoDB Java driver versions 2.10.0 to 2.14.2 that the feature supports are no longer in service. Instead of using the mongodb-2.0 feature, create a CDI producer for Mongo. The CDI producer can use any Mongo version that meets your requirements.

About this task

When you configure the mongo feature in your server.xml file, you can pass a collection of hostNames and ports that are either replica set members or sharded mongos servers.

If the host:port combinations are replica set members, the client finds all members and uses the main by default. If the combinations are sharded mongos servers, the client sends all requests to the closest member with the lowest ping time. If the closest member is down, the client automatically fails over to the next server.

Procedure

Configure the hostNames and ports in your server.xml file.
<mongo id="mongo1" libraryRef="MongoLib" hostNames="localhost,localhost,localhost" ports="9991,9992,9993"/>

Results

You configured a sharded MongoDB configuration.