Class MongoClient
- All Implemented Interfaces:
Closeable
,AutoCloseable
A MongoDB client with internal connection pooling. For most applications, you should have one MongoClient instance for the entire JVM.
The following are equivalent, and all connect to the local database running on the default port:
MongoClient mongoClient1 = new MongoClient(); MongoClient mongoClient1 = new MongoClient("localhost"); MongoClient mongoClient2 = new MongoClient("localhost", 27017); MongoClient mongoClient4 = new MongoClient(new ServerAddress("localhost")); MongoClient mongoClient5 = new MongoClient(new ServerAddress("localhost"), MongoClientOptions.builder().build());
You can connect to a replica set using the Java driver by passing a ServerAddress list to the MongoClient constructor. For example:
MongoClient mongoClient = new MongoClient(Arrays.asList( new ServerAddress("localhost", 27017), new ServerAddress("localhost", 27018), new ServerAddress("localhost", 27019)));
You can connect to a sharded cluster using the same constructor. MongoClient will auto-detect whether the servers are a list of replica set members or a list of mongos servers.
By default, all read and write operations will be made on the primary, but it's possible to read from secondaries by changing the read preference:
mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
By default, all write operations will wait for acknowledgment by the server, as the default write concern is
WriteConcern.ACKNOWLEDGED
.
Note: This class supersedes the Mongo
class. While it extends Mongo
, it differs from it in that the default write
concern is to wait for acknowledgment from the server of all write operations. In addition, its constructors accept instances of
MongoClientOptions
and MongoClientURI
, which both also set the same default write concern.
In general, users of this class will pick up all of the default options specified in MongoClientOptions
. In particular, note
that the default value of the connectionsPerHost option has been increased to 100 from the old default value of 10 used by the superseded
Mongo
class.
- Since:
- 2.10.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class com.mongodb.Mongo
Mongo.Holder
-
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.Creates an instance based on a (single) mongodb node (localhost, default port).Deprecated.Creates a Mongo described by a URI.MongoClient
(MongoClientURI uri, MongoDriverInformation mongoDriverInformation) Deprecated.Creates a Mongo described by a URI.MongoClient
(ServerAddress addr) Deprecated.Creates a Mongo instance based on a (single) mongodb nodeMongoClient
(ServerAddress addr, MongoClientOptions options) Deprecated.Creates a Mongo instance based on a (single) mongo node using a given ServerAddress and default options.MongoClient
(ServerAddress addr, MongoCredential credential, MongoClientOptions options) Deprecated.Creates a Mongo instance based on a (single) mongo node using a given server address, credential, and optionsMongoClient
(ServerAddress addr, MongoCredential credential, MongoClientOptions options, MongoDriverInformation mongoDriverInformation) Deprecated.Creates a MongoClient to a single node using a given ServerAddress.MongoClient
(ServerAddress addr, List<MongoCredential> credentialsList) Deprecated.MongoClient
(ServerAddress addr, List<MongoCredential> credentialsList, MongoClientOptions options) Deprecated.MongoClient
(ServerAddress addr, List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation) MongoClient
(String host) Deprecated.Creates a Mongo instance based on a (single) mongodb node.MongoClient
(String host, int port) Deprecated.Creates a Mongo instance based on a (single) mongodb node.MongoClient
(String host, MongoClientOptions options) Deprecated.Creates a Mongo instance based on a (single) mongodb node (default port).MongoClient
(List<ServerAddress> seeds) Deprecated.Creates an instance based on a list of replica set members or mongos servers.MongoClient
(List<ServerAddress> seeds, MongoClientOptions options) Deprecated.Construct an instance based on a list of replica set members or mongos servers.MongoClient
(List<ServerAddress> seeds, MongoCredential credential, MongoClientOptions options) Deprecated.Creates an instance based on a list of replica set members or mongos servers.MongoClient
(List<ServerAddress> seeds, MongoCredential credential, MongoClientOptions options, MongoDriverInformation mongoDriverInformation) Deprecated.Creates a MongoClientMongoClient
(List<ServerAddress> seeds, List<MongoCredential> credentialsList) Deprecated.MongoClient
(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options) Deprecated.MongoClient
(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Deprecated.Closes all resources associated with this instance, in particular any open network connections.Deprecated.Gets the credential that this client authenticates all connections withDeprecated.PrefergetCredential()
getDatabase
(String databaseName) Deprecated.static CodecRegistry
Deprecated.Gets the default codec registry.Deprecated.Gets the options that this client uses to connect to server.Deprecated.Get a list of the database nameslistDatabaseNames
(ClientSession clientSession) Deprecated.Get a list of the database namesDeprecated.Gets the list of databaseslistDatabases
(ClientSession clientSession) Deprecated.Gets the list of databases<T> ListDatabasesIterable
<T> listDatabases
(ClientSession clientSession, Class<T> clazz) Deprecated.Gets the list of databases<T> ListDatabasesIterable
<T> listDatabases
(Class<T> clazz) Deprecated.Gets the list of databasesDeprecated.Creates a client session with default session options.startSession
(ClientSessionOptions options) Deprecated.Creates a client session.watch()
Deprecated.Creates a change stream for this client.watch
(ClientSession clientSession) Deprecated.Creates a change stream for this client.<TResult> ChangeStreamIterable
<TResult> watch
(ClientSession clientSession, Class<TResult> resultClass) Deprecated.Creates a change stream for this client.watch
(ClientSession clientSession, List<? extends Bson> pipeline) Deprecated.Creates a change stream for this client.<TResult> ChangeStreamIterable
<TResult> watch
(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass) Deprecated.Creates a change stream for this client.<TResult> ChangeStreamIterable
<TResult> Deprecated.Creates a change stream for this client.Deprecated.Creates a change stream for this client.<TResult> ChangeStreamIterable
<TResult> Deprecated.Creates a change stream for this client.Methods inherited from class com.mongodb.Mongo
addOption, dropDatabase, fsync, fsyncAndLock, getAddress, getAllAddress, getConnectPoint, getDatabaseNames, getDB, getMaxBsonObjectSize, getMongoOptions, getOptions, getReadConcern, getReadPreference, getReplicaSetStatus, getServerAddressList, getUsedDatabases, getWriteConcern, isLocked, resetOptions, setOptions, setReadPreference, setWriteConcern, slaveOk, toString, unlock
-
Constructor Details
-
MongoClient
public MongoClient()Deprecated.Creates an instance based on a (single) mongodb node (localhost, default port). -
MongoClient
Deprecated.Creates a Mongo instance based on a (single) mongodb node.- Parameters:
host
- server to connect to in format host[:port]
-
MongoClient
Deprecated.Creates a Mongo instance based on a (single) mongodb node (default port).- Parameters:
host
- server to connect to in format host[:port]options
- default query options
-
MongoClient
Deprecated.Creates a Mongo instance based on a (single) mongodb node.- Parameters:
host
- the database's host addressport
- the port on which the database is running
-
MongoClient
Deprecated.Creates a Mongo instance based on a (single) mongodb node- Parameters:
addr
- the database address- See Also:
-
MongoClient
Deprecated.Creates a Mongo instance based on a (single) mongodb node and a list of credentials- Parameters:
addr
- the database addresscredentialsList
- the list of credentials used to authenticate all connections- Since:
- 2.11.0
- See Also:
-
MongoClient
Deprecated.Creates a Mongo instance based on a (single) mongo node using a given ServerAddress and default options.- Parameters:
addr
- the database addressoptions
- default options- See Also:
-
MongoClient
@Deprecated public MongoClient(ServerAddress addr, List<MongoCredential> credentialsList, MongoClientOptions options) Deprecated.Creates a Mongo instance based on a (single) mongo node using a given ServerAddress and default options.- Parameters:
addr
- the database addresscredentialsList
- the list of credentials used to authenticate all connectionsoptions
- default options- Since:
- 2.11.0
- See Also:
-
MongoClient
Deprecated.Creates a Mongo instance based on a (single) mongo node using a given server address, credential, and options- Parameters:
addr
- the database addresscredential
- the credential used to authenticate all connectionsoptions
- default options- Since:
- 3.6.0
- See Also:
-
MongoClient
Deprecated.Creates an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members. For a list with a single seed, the driver will still discover all members of the replica set. For a direct connection to a replica set member, with no discovery, use the
MongoClient(ServerAddress)
constructor instead.When there is more than one server to choose from based on the type of request (read or write) and the read preference (if it's a read request), the driver will randomly select a server to send a request. This applies to both replica sets and sharded clusters. The servers to randomly select from are further limited by the local threshold. See
MongoClientOptions.getLocalThreshold()
- Parameters:
seeds
- Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod servers in the same replica set or a list of mongos servers in the same sharded cluster.- See Also:
-
MongoClient
Deprecated.Creates an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members. For a list with a single seed, the driver will still discover all members of the replica set. For a direct connection to a replica set member, with no discovery, use the
MongoClient(ServerAddress, List)
constructor instead.When there is more than one server to choose from based on the type of request (read or write) and the read preference (if it's a read request), the driver will randomly select a server to send a request. This applies to both replica sets and sharded clusters. The servers to randomly select from are further limited by the local threshold. See
MongoClientOptions.getLocalThreshold()
- Parameters:
seeds
- Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod servers in the same replica set or a list of mongos servers in the same sharded cluster.credentialsList
- the list of credentials used to authenticate all connections- Since:
- 2.11.0
- See Also:
-
MongoClient
Deprecated.Construct an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members. For a list with a single seed, the driver will still discover all members of the replica set. For a direct connection to a replica set member, with no discovery, use the
MongoClient(ServerAddress, MongoClientOptions)
constructor instead.When there is more than one server to choose from based on the type of request (read or write) and the read preference (if it's a read request), the driver will randomly select a server to send a request. This applies to both replica sets and sharded clusters. The servers to randomly select from are further limited by the local threshold. See
MongoClientOptions.getLocalThreshold()
- Parameters:
seeds
- Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod servers in the same replica set or a list of mongos servers in the same sharded cluster.options
- the options- See Also:
-
MongoClient
@Deprecated public MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options) Deprecated.Creates an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members. For a list with a single seed, the driver will still discover all members of the replica set. For a direct connection to a replica set member, with no discovery, use the
MongoClient(ServerAddress, List, MongoClientOptions)
constructor instead.When there is more than one server to choose from based on the type of request (read or write) and the read preference (if it's a read request), the driver will randomly select a server to send a request. This applies to both replica sets and sharded clusters. The servers to randomly select from are further limited by the local threshold. See
MongoClientOptions.getLocalThreshold()
- Parameters:
seeds
- Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod servers in the same replica set or a list of mongos servers in the same sharded cluster.credentialsList
- the list of credentials used to authenticate all connectionsoptions
- the options- Since:
- 2.11.0
- See Also:
-
MongoClient
public MongoClient(List<ServerAddress> seeds, MongoCredential credential, MongoClientOptions options) Deprecated.Creates an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members. For a list with a single seed, the driver will still discover all members of the replica set. For a direct connection to a replica set member, with no discovery, use the
MongoClient(ServerAddress, List, MongoClientOptions)
constructor instead.When there is more than one server to choose from based on the type of request (read or write) and the read preference (if it's a read request), the driver will randomly select a server to send a request. This applies to both replica sets and sharded clusters. The servers to randomly select from are further limited by the local threshold. See
MongoClientOptions.getLocalThreshold()
- Parameters:
seeds
- Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod servers in the same replica set or a list of mongos servers in the same sharded cluster.credential
- the credential used to authenticate all connectionsoptions
- the options- Since:
- 3.6.0
- See Also:
-
MongoClient
Deprecated.Creates a Mongo described by a URI. If only one address is used it will only connect to that node, otherwise it will discover all nodes.- Parameters:
uri
- the URI- Throws:
MongoException
- if theres a failure
-
MongoClient
Deprecated.Creates a Mongo described by a URI.Note: Intended for driver and library authors to associate extra driver metadata with the connections.
- Parameters:
uri
- the URImongoDriverInformation
- any driver information to associate with the MongoClient- Throws:
MongoException
- if theres a failure- Since:
- 3.4
-
MongoClient
@Deprecated public MongoClient(ServerAddress addr, List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation) Deprecated.Creates a MongoClient to a single node using a given ServerAddress.Note: Intended for driver and library authors to associate extra driver metadata with the connections.
- Parameters:
addr
- the database addresscredentialsList
- the list of credentials used to authenticate all connectionsoptions
- default optionsmongoDriverInformation
- any driver information to associate with the MongoClient- Since:
- 3.4
- See Also:
-
MongoClient
public MongoClient(ServerAddress addr, MongoCredential credential, MongoClientOptions options, MongoDriverInformation mongoDriverInformation) Deprecated.Creates a MongoClient to a single node using a given ServerAddress.Note: Intended for driver and library authors to associate extra driver metadata with the connections.
- Parameters:
addr
- the database addresscredential
- the credential used to authenticate all connectionsoptions
- default optionsmongoDriverInformation
- any driver information to associate with the MongoClient- Since:
- 3.6
- See Also:
-
MongoClient
@Deprecated public MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation) Creates a MongoClientNote: Intended for driver and library authors to associate extra driver metadata with the connections.
- Parameters:
seeds
- Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod servers in the same replica set or a list of mongos servers in the same sharded cluster.credentialsList
- the list of credentials used to authenticate all connectionsoptions
- the optionsmongoDriverInformation
- any driver information to associate with the MongoClient- Since:
- 3.4
-
MongoClient
public MongoClient(List<ServerAddress> seeds, MongoCredential credential, MongoClientOptions options, MongoDriverInformation mongoDriverInformation) Deprecated.Creates a MongoClientNote: Intended for driver and library authors to associate extra driver metadata with the connections.
- Parameters:
seeds
- Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod servers in the same replica set or a list of mongos servers in the same sharded cluster.credential
- the credential used to authenticate all connectionsoptions
- the optionsmongoDriverInformation
- any driver information to associate with the MongoClient- Since:
- 3.6
-
-
Method Details
-
getDefaultCodecRegistry
Deprecated.Gets the default codec registry. It includes the following providers:- Returns:
- the default codec registry
- Since:
- 3.0
- See Also:
-
getMongoClientOptions
Deprecated.Gets the options that this client uses to connect to server.Note:
MongoClientOptions
is immutable.- Returns:
- the options
-
getCredential
Deprecated.Gets the credential that this client authenticates all connections with- Returns:
- the credential, which may be null in unsecured deployments
- Since:
- 3.9
-
getCredentialsList
Deprecated.PrefergetCredential()
Gets the list of credentials that this client authenticates all connections with- Returns:
- the list of credentials
- Since:
- 2.11.0
-
listDatabaseNames
Deprecated.Get a list of the database names- Returns:
- an iterable containing all the names of all the databases
- Since:
- 3.0
-
listDatabaseNames
Deprecated.Get a list of the database names- Parameters:
clientSession
- the client session with which to associate this operation- Returns:
- an iterable containing all the names of all the databases
- Since:
- 3.6
-
listDatabases
Deprecated.Gets the list of databases- Returns:
- the list of databases
- Since:
- 3.0
-
listDatabases
Deprecated.Gets the list of databases- Type Parameters:
T
- the type of the class to use instead ofDocument
.- Parameters:
clazz
- the class to cast the database documents to- Returns:
- the list of databases
- Since:
- 3.0
-
listDatabases
Deprecated.Gets the list of databases- Parameters:
clientSession
- the client session with which to associate this operation- Returns:
- the list of databases
- Since:
- 3.6
-
listDatabases
Deprecated.Gets the list of databases- Type Parameters:
T
- the type of the class to use instead ofDocument
.- Parameters:
clientSession
- the client session with which to associate this operationclazz
- the class to cast the database documents to- Returns:
- the list of databases
- Since:
- 3.6
-
getDatabase
Deprecated.- Parameters:
databaseName
- the name of the database to retrieve- Returns:
- a
MongoDatabase
representing the specified database - Throws:
IllegalArgumentException
- if databaseName is invalid- See Also:
-
startSession
Deprecated.Creates a client session with default session options.- Returns:
- the client session
- Throws:
MongoClientException
- if the MongoDB cluster to which this client is connected does not support sessions- Since:
- 3.8
-
startSession
Deprecated.Creates a client session.- Parameters:
options
- the options for the client session- Returns:
- the client session
- Throws:
MongoClientException
- if the MongoDB cluster to which this client is connected does not support sessions- Since:
- 3.6
-
watch
Deprecated.Creates a change stream for this client.- Returns:
- the change stream iterable
- Since:
- 3.8
-
watch
Deprecated.Creates a change stream for this client.- Type Parameters:
TResult
- the target document type of the iterable.- Parameters:
resultClass
- the class to decode each document into- Returns:
- the change stream iterable
- Since:
- 3.8
-
watch
Deprecated.Creates a change stream for this client.- Parameters:
pipeline
- the aggregation pipeline to apply to the change stream- Returns:
- the change stream iterable
- Since:
- 3.8
-
watch
public <TResult> ChangeStreamIterable<TResult> watch(List<? extends Bson> pipeline, Class<TResult> resultClass) Deprecated.Creates a change stream for this client.- Type Parameters:
TResult
- the target document type of the iterable.- Parameters:
pipeline
- the aggregation pipeline to apply to the change streamresultClass
- the class to decode each document into- Returns:
- the change stream iterable
- Since:
- 3.8
-
watch
Deprecated.Creates a change stream for this client.- Parameters:
clientSession
- the client session with which to associate this operation- Returns:
- the change stream iterable
- Since:
- 3.8
-
watch
public <TResult> ChangeStreamIterable<TResult> watch(ClientSession clientSession, Class<TResult> resultClass) Deprecated.Creates a change stream for this client.- Type Parameters:
TResult
- the target document type of the iterable.- Parameters:
clientSession
- the client session with which to associate this operationresultClass
- the class to decode each document into- Returns:
- the change stream iterable
- Since:
- 3.8
-
watch
public ChangeStreamIterable<Document> watch(ClientSession clientSession, List<? extends Bson> pipeline) Deprecated.Creates a change stream for this client.- Parameters:
clientSession
- the client session with which to associate this operationpipeline
- the aggregation pipeline to apply to the change stream- Returns:
- the change stream iterable
- Since:
- 3.8
-
watch
public <TResult> ChangeStreamIterable<TResult> watch(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass) Deprecated.Creates a change stream for this client.- Type Parameters:
TResult
- the target document type of the iterable.- Parameters:
clientSession
- the client session with which to associate this operationpipeline
- the aggregation pipeline to apply to the change streamresultClass
- the class to decode each document into- Returns:
- the change stream iterable
- Since:
- 3.8
-
close
public void close()Deprecated.Closes all resources associated with this instance, in particular any open network connections. Once called, this instance and any databases obtained from it can no longer be used.
-