- MongoDB Async Driver
- Tutorials
- Connect to MongoDB
Connect to MongoDB
To make a connection to a running MongoDB instance, use MongoClients.create
to create a new MongoClient
instance.
A MongoClient
instance actually represents a pool of connections
to the database; you will only need one instance of class
MongoClient
even with multiple concurrently executing asynchronous operations.
important
Typically you only create one MongoClient
instance for a given MongoDB
deployment (e.g. standalone, replica set, or a sharded cluster) and use it across your application. However, if you do create multiple instances:
- All resource usage limits (max connections, etc.) apply per
MongoClient
instance. - To dispose of an instance, call
MongoClient.close()
to clean up resources.
Note
The 3.5 release deprecated socket keep-alive settings, also socket keep-alive checks are now on by default. It is strongly recommended that system keep-alive settings should be configured with shorter timeouts.
See the ‘does TCP keep-alive time affect MongoDB deployments?’ documentation for more information.
Prerequisites
Running MongoDB deployments to which to connect. For example, to connect to a standalone, you must have a running standalone.
The MongoDB Asynchronous Driver. See Installation for instructions on how to install the MongoDB async driver.
The following import statements:
import com.mongodb.ConnectionString; import com.mongodb.ServerAddress; import com.mongodb.async.client.*; import com.mongodb.connection.ClusterSettings; import java.util.Arrays; import static java.util.Arrays.asList;
Connect to a Standalone MongoDB Instance
To connect to a standalone MongoDB instance:
You can call
MongoClients.create()
without any parameters to connect to a MongoDB instance running on localhost on port27017
:MongoClient mongoClient = MongoClients.create();
You can call
MongoClients.create()
with a string that specifies the connection string:MongoClient mongoClient = MongoClients.create("mongodb://localhost");
The connection string mostly follows RFC 3986, with the exception of the domain name. For MongoDB, it is possible to list multiple domain names separated by a comma. For more information on the connection string, see connection string.
You can call
MongoClients.create()
with aConnectionString
object:MongoClient mongoClient = MongoClients.create(new ConnectionString("mongodb://localhost"));
You can call
MongoClients.create()
with aMongoClientSettings
object. To specify the host information, use theClusterSettings
.ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(asList(new ServerAddress("localhost"))) .build(); MongoClientSettings settings = MongoClientSettings.builder() .clusterSettings(clusterSettings).build(); MongoClient mongoClient = MongoClients.create(settings);
tip
MongoClientSettings
provide more configuration options than a connection string.
Connect to a Replica Set
To connect to a replica set, specify at least one or more members of the replica set in the connection string or MongoClientSettings
object and pass to MongoClients.create()
.
Note
MongoDB will auto-discover the primary and the secondaries.
You can call
MongoClients.create()
with a connection string that specifies the members of the replica set:- Specify at least two members of the replica set if you are not specifying the replica set name
MongoClient mongoClient = MongoClients.create( "mongodb://host1:27017,host2:27017,host3:27017");
- Specify at least one member of the replica set and the replica set name
MongoClient mongoClient = MongoClients.create( "mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet");
You can call
MongoClients.create()
with aConnectionString
object that specifies the members of the replica set:- Specify at least two members of the replica set if you are not specifying the replica set name
MongoClient mongoClient = MongoClients.create( new ConnectionString("mongodb://host1:27017,host2:27017,host3:27017"));
- Specify at least one member of the replica set and the replica set name:
MongoClient mongoClient = MongoClients.create( new ConnectionString("mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplicaSet"));
You can call
MongoClients.create()
with aMongoClientSettings
object. To specify the host information of the replica set members, useClusterSettings
.ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(asList( new ServerAddress("host1", 27017), new ServerAddress("host2", 27017), new ServerAddress("host3", 27017))) .build(); MongoClientSettings settings = MongoClientSettings.builder() .clusterSettings(clusterSettings).build(); MongoClient mongoClient = MongoClients.create(settings);
Connect to a Sharded Cluster
To connect to a sharded cluster, specify the mongos
instance or instances to the MongoClient
constructor.
To connect to a single mongos
instance:
You can call
MongoClients.create()
without any parameters to connect to a :program:mongos
running on localhost on port27017
:MongoClient mongoClient = MongoClients.create();
You can call
MongoClients.create()
with a string that specifies the host information of themongos
instance in the connection URI:MongoClient mongoClient = MongoClients.create("mongodb://localhost");
You can call
MongoClients.create()
with aConnectionString
object that specifies the host information of themongos
instance:MongoClient mongoClient = MongoClients.create( new ConnectionString("mongodb://localhost"));
You can call
MongoClients.create()
with aMongoClientSettings
object. To specify the host information of themongos
instance, useClusterSettings
:ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(asList(new ServerAddress("localhost"))) .build(); MongoClientSettings settings = MongoClientSettings.builder() .clusterSettings(clusterSettings) .build(); MongoClient mongoClient = MongoClients.create(settings);
To connect to multiple mongos
instances, specify the host and port of the mongos
instances:
You can call
MongoClients.create()
with a string that specifies the host and port information of themongos
instances in the connection URI:MongoClient mongoClient = MongoClients.create("mongodb://host1:27017,host2:27017");
You can call
MongoClients.create()
with aConnectionString
object that specifies the host and port information of themongos
instances:MongoClient mongoClient = MongoClients.create( new ConnectionString("mongodb://host1:27017,host2:27017"));
You can call
MongoClients.create()
with aMongoClientSettings
object. To specify the host information of themongos
instances, useClusterSettings
:ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(asList( new ServerAddress("host1", 27017), new ServerAddress("host2", 27017))) .build(); MongoClientSettings settings = MongoClientSettings.builder() .clusterSettings(clusterSettings).build(); MongoClient mongoClient = MongoClients.create(settings);
Connection Options
You can specify the connection settings using either the
connection string (or ConnectionString
object) or the MongoClientSettings
or both.
Netty Configuration
Note
Netty is an optional dependency of the asynchronous driver. If your application requires Netty, it must explicitly add a dependency to Netty artifacts. The driver is currently tested against Netty 4.1.
In its default configuration, the async driver is supported on Java 7+ when TLS/SSL is disabled, and on Java 8+ when TLS/SSL is enabled.
Otherwise, applications must use Netty.
To configure the driver to use Netty,
Include the
streamType
option set tonetty
in the connection stringMongoClient client = MongoClients.create("mongodb://localhost/?streamType=netty");
Configure
MongoClientSettings
with theStreamFactory
set to use Netty:MongoClient client = MongoClients.create(MongoClientSettings.builder() .applyConnectionString(new ConnectionString("mongodb://localhost")) .streamFactoryFactory(NettyStreamFactoryFactory.builder().build()) .build());
Note
The streamType connection string query parameter is deprecated as of the 3.10 release and will be removed in the next major release.
Note
Netty may also be configured by setting the org.mongodb.async.type
system property to netty
, but this should be considered as
deprecated as of the 3.1 driver release, and will be removed in the next major release.