Closeable
, AutoCloseable
public class MongoClient extends Mongo implements Closeable
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.
ReadPreference.primary()
,
WriteConcern.ACKNOWLEDGED
,
MongoClientOptions
,
MongoClientURI
Mongo.Holder
Constructor | Description |
---|---|
MongoClient() |
Creates an instance based on a (single) mongodb node (localhost, default port).
|
MongoClient(MongoClientURI uri) |
Creates a Mongo described by a URI.
|
MongoClient(MongoClientURI uri,
MongoDriverInformation mongoDriverInformation) |
Creates a Mongo described by a URI.
|
MongoClient(ServerAddress addr) |
Creates a Mongo instance based on a (single) mongodb node
|
MongoClient(ServerAddress addr,
MongoClientOptions options) |
Creates a Mongo instance based on a (single) mongo node using a given ServerAddress and default options.
|
MongoClient(ServerAddress addr,
MongoCredential credential,
MongoClientOptions options) |
Creates a Mongo instance based on a (single) mongo node using a given server address, credential, and options
|
MongoClient(ServerAddress addr,
MongoCredential credential,
MongoClientOptions options,
MongoDriverInformation mongoDriverInformation) |
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) |
Creates a Mongo instance based on a (single) mongodb node.
|
MongoClient(String host,
int port) |
Creates a Mongo instance based on a (single) mongodb node.
|
MongoClient(String host,
MongoClientOptions options) |
Creates a Mongo instance based on a (single) mongodb node (default port).
|
MongoClient(List<ServerAddress> seeds) |
Creates an instance based on a list of replica set members or mongos servers.
|
MongoClient(List<ServerAddress> seeds,
MongoClientOptions options) |
Construct an instance based on a list of replica set members or mongos servers.
|
MongoClient(List<ServerAddress> seeds,
MongoCredential credential,
MongoClientOptions options) |
Creates an instance based on a list of replica set members or mongos servers.
|
MongoClient(List<ServerAddress> seeds,
MongoCredential credential,
MongoClientOptions options,
MongoDriverInformation mongoDriverInformation) |
Creates a MongoClient
|
MongoClient(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) |
Modifier and Type | Method | Description |
---|---|---|
List<MongoCredential> |
getCredentialsList() |
Gets the list of credentials that this client authenticates all connections with
|
MongoDatabase |
getDatabase(String databaseName) |
|
static CodecRegistry |
getDefaultCodecRegistry() |
Gets the default codec registry.
|
MongoClientOptions |
getMongoClientOptions() |
Gets the options that this client uses to connect to server.
|
MongoIterable<String> |
listDatabaseNames() |
Get a list of the database names
|
MongoIterable<String> |
listDatabaseNames(ClientSession clientSession) |
Get a list of the database names
|
ListDatabasesIterable<Document> |
listDatabases() |
Gets the list of databases
|
ListDatabasesIterable<Document> |
listDatabases(ClientSession clientSession) |
Gets the list of databases
|
<T> ListDatabasesIterable<T> |
listDatabases(ClientSession clientSession,
Class<T> clazz) |
Gets the list of databases
|
<T> ListDatabasesIterable<T> |
listDatabases(Class<T> clazz) |
Gets the list of databases
|
ClientSession |
startSession() |
Creates a client session with default session options.
|
ClientSession |
startSession(ClientSessionOptions options) |
Creates a client session.
|
ChangeStreamIterable<Document> |
watch() |
Creates a change stream for this client.
|
ChangeStreamIterable<Document> |
watch(ClientSession clientSession) |
Creates a change stream for this client.
|
<TResult> ChangeStreamIterable<TResult> |
watch(ClientSession clientSession,
Class<TResult> resultClass) |
Creates a change stream for this client.
|
ChangeStreamIterable<Document> |
watch(ClientSession clientSession,
List<? extends Bson> pipeline) |
Creates a change stream for this client.
|
<TResult> ChangeStreamIterable<TResult> |
watch(ClientSession clientSession,
List<? extends Bson> pipeline,
Class<TResult> resultClass) |
Creates a change stream for this client.
|
<TResult> ChangeStreamIterable<TResult> |
watch(Class<TResult> resultClass) |
Creates a change stream for this client.
|
ChangeStreamIterable<Document> |
watch(List<? extends Bson> pipeline) |
Creates a change stream for this client.
|
<TResult> ChangeStreamIterable<TResult> |
watch(List<? extends Bson> pipeline,
Class<TResult> resultClass) |
Creates a change stream for this client.
|
addOption, close, 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
public MongoClient()
public MongoClient(String host)
host
- server to connect to in format host[:port]public MongoClient(String host, MongoClientOptions options)
host
- server to connect to in format host[:port]options
- default query optionspublic MongoClient(String host, int port)
host
- the database's host addressport
- the port on which the database is runningpublic MongoClient(ServerAddress addr)
addr
- the database addressServerAddress
@Deprecated public MongoClient(ServerAddress addr, List<MongoCredential> credentialsList)
MongoClient(ServerAddress, MongoCredential, MongoClientOptions)
addr
- the database addresscredentialsList
- the list of credentials used to authenticate all connectionsServerAddress
public MongoClient(ServerAddress addr, MongoClientOptions options)
addr
- the database addressoptions
- default optionsServerAddress
@Deprecated public MongoClient(ServerAddress addr, List<MongoCredential> credentialsList, MongoClientOptions options)
MongoClient(ServerAddress, MongoCredential, MongoClientOptions)
addr
- the database addresscredentialsList
- the list of credentials used to authenticate all connectionsoptions
- default optionsServerAddress
public MongoClient(ServerAddress addr, MongoCredential credential, MongoClientOptions options)
addr
- the database addresscredential
- the credential used to authenticate all connectionsoptions
- default optionsServerAddress
public MongoClient(List<ServerAddress> seeds)
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()
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.MongoClientOptions.getLocalThreshold()
@Deprecated public MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList)
MongoClient(List, MongoCredential, MongoClientOptions)
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()
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 connectionsMongoClientOptions.getLocalThreshold()
public MongoClient(List<ServerAddress> seeds, MongoClientOptions options)
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()
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 optionsMongoClientOptions.getLocalThreshold()
@Deprecated public MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options)
MongoClient(List, MongoCredential, MongoClientOptions)
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()
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 optionsMongoClientOptions.getLocalThreshold()
public MongoClient(List<ServerAddress> seeds, MongoCredential credential, MongoClientOptions options)
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()
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 optionsMongoClientOptions.getLocalThreshold()
public MongoClient(MongoClientURI uri)
uri
- the URIMongoException
- if theres a failurepublic MongoClient(MongoClientURI uri, MongoDriverInformation mongoDriverInformation)
Note: Intended for driver and library authors to associate extra driver metadata with the connections.
uri
- the URImongoDriverInformation
- any driver information to associate with the MongoClientMongoException
- if theres a failure@Deprecated public MongoClient(ServerAddress addr, List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation)
MongoClient(ServerAddress, MongoCredential, MongoClientOptions, MongoDriverInformation)
Note: Intended for driver and library authors to associate extra driver metadata with the connections.
addr
- the database addresscredentialsList
- the list of credentials used to authenticate all connectionsoptions
- default optionsmongoDriverInformation
- any driver information to associate with the MongoClientServerAddress
public MongoClient(ServerAddress addr, MongoCredential credential, MongoClientOptions options, MongoDriverInformation mongoDriverInformation)
Note: Intended for driver and library authors to associate extra driver metadata with the connections.
addr
- the database addresscredential
- the credential used to authenticate all connectionsoptions
- default optionsmongoDriverInformation
- any driver information to associate with the MongoClientServerAddress
@Deprecated public MongoClient(List<ServerAddress> seeds, List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation)
Note: Intended for driver and library authors to associate extra driver metadata with the connections.
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 MongoClientpublic MongoClient(List<ServerAddress> seeds, MongoCredential credential, MongoClientOptions options, MongoDriverInformation mongoDriverInformation)
Note: Intended for driver and library authors to associate extra driver metadata with the connections.
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 MongoClientpublic static CodecRegistry getDefaultCodecRegistry()
MongoClientOptions.getCodecRegistry()
public MongoClientOptions getMongoClientOptions()
Note: MongoClientOptions
is immutable.
public List<MongoCredential> getCredentialsList()
public MongoIterable<String> listDatabaseNames()
public MongoIterable<String> listDatabaseNames(ClientSession clientSession)
clientSession
- the client session with which to associate this operationpublic ListDatabasesIterable<Document> listDatabases()
public <T> ListDatabasesIterable<T> listDatabases(Class<T> clazz)
T
- the type of the class to use instead of Document
.clazz
- the class to cast the database documents topublic ListDatabasesIterable<Document> listDatabases(ClientSession clientSession)
clientSession
- the client session with which to associate this operationpublic <T> ListDatabasesIterable<T> listDatabases(ClientSession clientSession, Class<T> clazz)
T
- the type of the class to use instead of Document
.clientSession
- the client session with which to associate this operationclazz
- the class to cast the database documents topublic MongoDatabase getDatabase(String databaseName)
databaseName
- the name of the database to retrieveMongoDatabase
representing the specified databaseIllegalArgumentException
- if databaseName is invalidMongoNamespace.checkDatabaseNameValidity(String)
public ClientSession startSession()
MongoClientException
- if the MongoDB cluster to which this client is connected does not support sessionspublic ClientSession startSession(ClientSessionOptions options)
options
- the options for the client sessionMongoClientException
- if the MongoDB cluster to which this client is connected does not support sessionspublic ChangeStreamIterable<Document> watch()
public <TResult> ChangeStreamIterable<TResult> watch(Class<TResult> resultClass)
TResult
- the target document type of the iterable.resultClass
- the class to decode each document intopublic ChangeStreamIterable<Document> watch(List<? extends Bson> pipeline)
pipeline
- the aggregation pipeline to apply to the change streampublic <TResult> ChangeStreamIterable<TResult> watch(List<? extends Bson> pipeline, Class<TResult> resultClass)
TResult
- the target document type of the iterable.pipeline
- the aggregation pipeline to apply to the change streamresultClass
- the class to decode each document intopublic ChangeStreamIterable<Document> watch(ClientSession clientSession)
clientSession
- the client session with which to associate this operationpublic <TResult> ChangeStreamIterable<TResult> watch(ClientSession clientSession, Class<TResult> resultClass)
TResult
- the target document type of the iterable.clientSession
- the client session with which to associate this operationresultClass
- the class to decode each document intopublic ChangeStreamIterable<Document> watch(ClientSession clientSession, List<? extends Bson> pipeline)
clientSession
- the client session with which to associate this operationpipeline
- the aggregation pipeline to apply to the change streampublic <TResult> ChangeStreamIterable<TResult> watch(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass)
TResult
- the target document type of the iterable.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