Interface MongoClient
- All Superinterfaces:
AutoCloseable
,Closeable
A client-side representation of a MongoDB cluster. Instances can represent either a standalone MongoDB instance, a replica set,
or a sharded cluster. Instance of this class are responsible for maintaining an up-to-date state of the cluster,
and possibly cache resources related to this, including background threads for monitoring, and connection pools.
Instance of this class server as factories for MongoDatabase
instances.
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Close the client, which will close all underlying cached resources, including, for example, sockets and background monitoring threads.Gets the current cluster description.getDatabase
(String name) Gets the database with the given name.Get a list of the database nameslistDatabaseNames
(ClientSession clientSession) Get a list of the database namesGets the list of databaseslistDatabases
(ClientSession clientSession) Gets the list of databases<TResult> ListDatabasesPublisher<TResult>
listDatabases
(ClientSession clientSession, Class<TResult> clazz) Gets the list of databases<TResult> ListDatabasesPublisher<TResult>
listDatabases
(Class<TResult> clazz) Gets the list of databasesCreates a client session.startSession
(ClientSessionOptions options) Creates a client session.watch()
Creates a change stream for this client.watch
(ClientSession clientSession) Creates a change stream for this client.<TResult> ChangeStreamPublisher<TResult>
watch
(ClientSession clientSession, Class<TResult> resultClass) Creates a change stream for this client.watch
(ClientSession clientSession, List<? extends Bson> pipeline) Creates a change stream for this client.<TResult> ChangeStreamPublisher<TResult>
watch
(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass) Creates a change stream for this client.<TResult> ChangeStreamPublisher<TResult>
Creates a change stream for this client.Creates a change stream for this client.<TResult> ChangeStreamPublisher<TResult>
Creates a change stream for this client.
-
Method Details
-
getDatabase
Gets the database with the given name.- Parameters:
name
- the name of the database- Returns:
- the database
-
close
void close()Close the client, which will close all underlying cached resources, including, for example, sockets and background monitoring threads.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
listDatabaseNames
Get a list of the database names- Returns:
- an iterable containing all the names of all the databases
- MongoDB documentation
- List Databases
-
listDatabaseNames
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:
- 1.7
- MongoDB documentation
- List Databases
- Since server release
- 3.6
-
listDatabases
ListDatabasesPublisher<Document> listDatabases()Gets the list of databases- Returns:
- the fluent list databases interface
-
listDatabases
Gets the list of databases- Type Parameters:
TResult
- the type of the class to use instead ofDocument
.- Parameters:
clazz
- the class to cast the database documents to- Returns:
- the fluent list databases interface
-
listDatabases
Gets the list of databases- Parameters:
clientSession
- the client session with which to associate this operation- Returns:
- the fluent list databases interface
- Since:
- 1.7
- Since server release
- 3.6
-
listDatabases
<TResult> ListDatabasesPublisher<TResult> listDatabases(ClientSession clientSession, Class<TResult> clazz) Gets the list of databases- Type Parameters:
TResult
- 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 fluent list databases interface
- Since:
- 1.7
- Since server release
- 3.6
-
watch
ChangeStreamPublisher<Document> watch()Creates a change stream for this client.- Returns:
- the change stream iterable
- Since:
- 1.9
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
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:
- 1.9
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
Creates a change stream for this client.- Parameters:
pipeline
- the aggregation pipeline to apply to the change stream.- Returns:
- the change stream iterable
- Since:
- 1.9
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
<TResult> ChangeStreamPublisher<TResult> watch(List<? extends Bson> pipeline, Class<TResult> resultClass) 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:
- 1.9
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
Creates a change stream for this client.- Parameters:
clientSession
- the client session with which to associate this operation- Returns:
- the change stream iterable
- Since:
- 1.9
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
<TResult> ChangeStreamPublisher<TResult> watch(ClientSession clientSession, Class<TResult> resultClass) 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:
- 1.9
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
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:
- 1.9
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
<TResult> ChangeStreamPublisher<TResult> watch(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass) 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:
- 1.9
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
startSession
Publisher<ClientSession> startSession()Creates a client session.- Returns:
- a publisher for the client session.
- Since:
- 1.9
- Since server release
- 3.6
-
startSession
Creates a client session.- Parameters:
options
- the options for the client session- Returns:
- a publisher for the client session.
- Since:
- 1.7
- Since server release
- 3.6
-
getClusterDescription
ClusterDescription getClusterDescription()Gets the current cluster description.This method will not block, meaning that it may return a
ClusterDescription
whoseclusterType
is unknown and whoseServerDescription
s are all in the connecting state. If the application requires notifications after the driver has connected to a member of the cluster, it should register aClusterListener
via theClusterSettings
inMongoClientSettings
.- Returns:
- the current cluster description
- Since:
- 4.1
- See Also:
-