Interface MongoDatabase
Note: Additions to this interface will not be considered to break binary compatibility.
- Since:
- 3.0
-
Method Summary
Modifier and TypeMethodDescriptionaggregate
(ClientSession clientSession, List<? extends Bson> pipeline) Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as$currentOp
and$listLocalSessions
.<TResult> AggregateIterable<TResult>
aggregate
(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass) Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as$currentOp
and$listLocalSessions
.Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as$currentOp
and$listLocalSessions
.<TResult> AggregateIterable<TResult>
Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as$currentOp
and$listLocalSessions
.void
createCollection
(ClientSession clientSession, String collectionName) Create a new collection with the given name.void
createCollection
(ClientSession clientSession, String collectionName, CreateCollectionOptions createCollectionOptions) Create a new collection with the selected optionsvoid
createCollection
(String collectionName) Create a new collection with the given name.void
createCollection
(String collectionName, CreateCollectionOptions createCollectionOptions) Create a new collection with the selected optionsvoid
createView
(ClientSession clientSession, String viewName, String viewOn, List<? extends Bson> pipeline) Creates a view with the given name, backing collection/view name, and aggregation pipeline that defines the view.void
createView
(ClientSession clientSession, String viewName, String viewOn, List<? extends Bson> pipeline, CreateViewOptions createViewOptions) Creates a view with the given name, backing collection/view name, aggregation pipeline, and options that defines the view.void
createView
(String viewName, String viewOn, List<? extends Bson> pipeline) Creates a view with the given name, backing collection/view name, and aggregation pipeline that defines the view.void
createView
(String viewName, String viewOn, List<? extends Bson> pipeline, CreateViewOptions createViewOptions) Creates a view with the given name, backing collection/view name, aggregation pipeline, and options that defines the view.void
drop()
Drops this database.void
drop
(ClientSession clientSession) Drops this database.Get the codec registry for the MongoDatabase.getCollection
(String collectionName) Gets a collection.<TDocument>
MongoCollection<TDocument>getCollection
(String collectionName, Class<TDocument> documentClass) Gets a collection, with a specific default document class.getName()
Gets the name of the database.Get the read concern for the MongoDatabase.Get the read preference for the MongoDatabase.getTimeout
(TimeUnit timeUnit) The time limit for the full execution of an operation.Get the write concern for the MongoDatabase.Gets the names of all the collections in this database.listCollectionNames
(ClientSession clientSession) Gets the names of all the collections in this database.Finds all the collections in this database.listCollections
(ClientSession clientSession) Finds all the collections in this database.<TResult> ListCollectionsIterable<TResult>
listCollections
(ClientSession clientSession, Class<TResult> resultClass) Finds all the collections in this database.<TResult> ListCollectionsIterable<TResult>
listCollections
(Class<TResult> resultClass) Finds all the collections in this database.runCommand
(ClientSession clientSession, Bson command) Executes the given command in the context of the current database with a read preference ofReadPreference.primary()
.runCommand
(ClientSession clientSession, Bson command, ReadPreference readPreference) Executes the given command in the context of the current database with the given read preference.<TResult> TResult
runCommand
(ClientSession clientSession, Bson command, ReadPreference readPreference, Class<TResult> resultClass) Executes the given command in the context of the current database with the given read preference.<TResult> TResult
runCommand
(ClientSession clientSession, Bson command, Class<TResult> resultClass) Executes the given command in the context of the current database with a read preference ofReadPreference.primary()
.runCommand
(Bson command) Executes the given command in the context of the current database with a read preference ofReadPreference.primary()
.runCommand
(Bson command, ReadPreference readPreference) Executes the given command in the context of the current database with the given read preference.<TResult> TResult
runCommand
(Bson command, ReadPreference readPreference, Class<TResult> resultClass) Executes the given command in the context of the current database with the given read preference.<TResult> TResult
runCommand
(Bson command, Class<TResult> resultClass) Executes the given command in the context of the current database with a read preference ofReadPreference.primary()
.watch()
Creates a change stream for this database.watch
(ClientSession clientSession) Creates a change stream for this database.<TResult> ChangeStreamIterable<TResult>
watch
(ClientSession clientSession, Class<TResult> resultClass) Creates a change stream for this database.watch
(ClientSession clientSession, List<? extends Bson> pipeline) Creates a change stream for this database.<TResult> ChangeStreamIterable<TResult>
watch
(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass) Creates a change stream for this database.<TResult> ChangeStreamIterable<TResult>
Creates a change stream for this database.Creates a change stream for this database.<TResult> ChangeStreamIterable<TResult>
Creates a change stream for this database.withCodecRegistry
(CodecRegistry codecRegistry) Create a new MongoDatabase instance with a different codec registry.withReadConcern
(ReadConcern readConcern) Create a new MongoDatabase instance with a different read concern.withReadPreference
(ReadPreference readPreference) Create a new MongoDatabase instance with a different read preference.withTimeout
(long timeout, TimeUnit timeUnit) Create a new MongoDatabase instance with the set time limit for the full execution of an operation.withWriteConcern
(WriteConcern writeConcern) Create a new MongoDatabase instance with a different write concern.
-
Method Details
-
getName
String getName()Gets the name of the database.- Returns:
- the database name
-
getCodecRegistry
CodecRegistry getCodecRegistry()Get the codec registry for the MongoDatabase.- Returns:
- the
CodecRegistry
-
getReadPreference
ReadPreference getReadPreference()Get the read preference for the MongoDatabase.- Returns:
- the
ReadPreference
-
getWriteConcern
WriteConcern getWriteConcern()Get the write concern for the MongoDatabase.- Returns:
- the
WriteConcern
-
getReadConcern
ReadConcern getReadConcern()Get the read concern for the MongoDatabase.- Returns:
- the
ReadConcern
- Since:
- 3.2
- MongoDB documentation
- Read Concern
- Since server release
- 3.2
-
getTimeout
The time limit for the full execution of an operation.If not null the following deprecated options will be ignored:
waitQueueTimeoutMS
,socketTimeoutMS
,wTimeoutMS
,maxTimeMS
andmaxCommitTimeMS
null
means that the timeout mechanism for operations will defer to using:waitQueueTimeoutMS
: The maximum wait time in milliseconds that a thread may wait for a connection to become availablesocketTimeoutMS
: How long a send or receive on a socket can take before timing out.wTimeoutMS
: How long the server will wait for the write concern to be fulfilled before timing out.maxTimeMS
: The cumulative time limit for processing operations on a cursor. See: cursor.maxTimeMS.maxCommitTimeMS
: The maximum amount of time to allow a singlecommitTransaction
command to execute. See:TransactionOptions.getMaxCommitTime(java.util.concurrent.TimeUnit)
.
0
means infinite timeout.> 0
The time limit to use for the full execution of an operation.
- Parameters:
timeUnit
- the time unit- Returns:
- the timeout in the given time unit
- Since:
- 5.2
-
withCodecRegistry
Create a new MongoDatabase instance with a different codec registry.The
CodecRegistry
configured by this method is effectively treated by the driver as an instance ofCodecProvider
, whichCodecRegistry
extends. So there is no benefit to defining a class that implementsCodecRegistry
. Rather, an application should always createCodecRegistry
instances using the factory methods inCodecRegistries
.- Parameters:
codecRegistry
- the newCodecRegistry
for the database- Returns:
- a new MongoDatabase instance with the different codec registry
- See Also:
-
withReadPreference
Create a new MongoDatabase instance with a different read preference.- Parameters:
readPreference
- the newReadPreference
for the database- Returns:
- a new MongoDatabase instance with the different readPreference
-
withWriteConcern
Create a new MongoDatabase instance with a different write concern.- Parameters:
writeConcern
- the newWriteConcern
for the database- Returns:
- a new MongoDatabase instance with the different writeConcern
-
withReadConcern
Create a new MongoDatabase instance with a different read concern.- Parameters:
readConcern
- the newReadConcern
for the database- Returns:
- a new MongoDatabase instance with the different ReadConcern
- Since:
- 3.2
- MongoDB documentation
- Read Concern
- Since server release
- 3.2
-
withTimeout
Create a new MongoDatabase instance with the set time limit for the full execution of an operation.0
means infinite timeout.> 0
The time limit to use for the full execution of an operation.
- Parameters:
timeout
- the timeout, which must be greater than or equal to 0timeUnit
- the time unit- Returns:
- a new MongoDatabase instance with the set time limit for the full execution of an operation.
- Since:
- 5.2
- See Also:
-
getCollection
Gets a collection.- Parameters:
collectionName
- the name of the collection to return- Returns:
- the collection
- Throws:
IllegalArgumentException
- if collectionName is invalid- See Also:
-
getCollection
<TDocument> MongoCollection<TDocument> getCollection(String collectionName, Class<TDocument> documentClass) Gets a collection, with a specific default document class.- Type Parameters:
TDocument
- the type of the class to use instead ofDocument
.- Parameters:
collectionName
- the name of the collection to returndocumentClass
- the default class to cast any documents returned from the database into.- Returns:
- the collection
-
runCommand
Executes the given command in the context of the current database with a read preference ofReadPreference.primary()
.Note: The behavior of
runCommand
is undefined if the provided command document includes amaxTimeMS
field and thetimeoutMS
setting has been set.- Parameters:
command
- the command to be run- Returns:
- the command result
-
runCommand
Executes the given command in the context of the current database with the given read preference.Note: The behavior of
runCommand
is undefined if the provided command document includes amaxTimeMS
field and thetimeoutMS
setting has been set.- Parameters:
command
- the command to be runreadPreference
- theReadPreference
to be used when executing the command- Returns:
- the command result
-
runCommand
Executes the given command in the context of the current database with a read preference ofReadPreference.primary()
.Note: The behavior of
runCommand
is undefined if the provided command document includes amaxTimeMS
field and thetimeoutMS
setting has been set.- Type Parameters:
TResult
- the type of the class to use instead ofDocument
.- Parameters:
command
- the command to be runresultClass
- the class to decode each document into- Returns:
- the command result
-
runCommand
<TResult> TResult runCommand(Bson command, ReadPreference readPreference, Class<TResult> resultClass) Executes the given command in the context of the current database with the given read preference.Note: The behavior of
runCommand
is undefined if the provided command document includes amaxTimeMS
field and thetimeoutMS
setting has been set.- Type Parameters:
TResult
- the type of the class to use instead ofDocument
.- Parameters:
command
- the command to be runreadPreference
- theReadPreference
to be used when executing the commandresultClass
- the class to decode each document into- Returns:
- the command result
-
runCommand
Executes the given command in the context of the current database with a read preference ofReadPreference.primary()
.Note: The behavior of
runCommand
is undefined if the provided command document includes amaxTimeMS
field and thetimeoutMS
setting has been set.- Parameters:
clientSession
- the client session with which to associate this operationcommand
- the command to be run- Returns:
- the command result
- Since:
- 3.6
- Since server release
- 3.6
-
runCommand
Executes the given command in the context of the current database with the given read preference.Note: The behavior of
runCommand
is undefined if the provided command document includes amaxTimeMS
field and thetimeoutMS
setting has been set.- Parameters:
clientSession
- the client session with which to associate this operationcommand
- the command to be runreadPreference
- theReadPreference
to be used when executing the command- Returns:
- the command result
- Since:
- 3.6
- Since server release
- 3.6
-
runCommand
Executes the given command in the context of the current database with a read preference ofReadPreference.primary()
.Note: The behavior of
runCommand
is undefined if the provided command document includes amaxTimeMS
field and thetimeoutMS
setting has been set.- Type Parameters:
TResult
- the type of the class to use instead ofDocument
.- Parameters:
clientSession
- the client session with which to associate this operationcommand
- the command to be runresultClass
- the class to decode each document into- Returns:
- the command result
- Since:
- 3.6
- Since server release
- 3.6
-
runCommand
<TResult> TResult runCommand(ClientSession clientSession, Bson command, ReadPreference readPreference, Class<TResult> resultClass) Executes the given command in the context of the current database with the given read preference.Note: The behavior of
runCommand
is undefined if the provided command document includes amaxTimeMS
field and thetimeoutMS
setting has been set.- Type Parameters:
TResult
- the type of the class to use instead ofDocument
.- Parameters:
clientSession
- the client session with which to associate this operationcommand
- the command to be runreadPreference
- theReadPreference
to be used when executing the commandresultClass
- the class to decode each document into- Returns:
- the command result
- Since:
- 3.6
- Since server release
- 3.6
-
drop
void drop()Drops this database.- MongoDB documentation
- Drop database
-
drop
Drops this database.- Parameters:
clientSession
- the client session with which to associate this operation- Since:
- 3.6
- MongoDB documentation
- Drop database
- Since server release
- 3.6
-
listCollectionNames
ListCollectionNamesIterable listCollectionNames()Gets the names of all the collections in this database.- Returns:
- an iterable containing all the names of all the collections in this database
- MongoDB documentation
- listCollections
-
listCollections
ListCollectionsIterable<Document> listCollections()Finds all the collections in this database.- Returns:
- the list collections iterable interface
- MongoDB documentation
- listCollections
-
listCollections
Finds all the collections in this database.- Type Parameters:
TResult
- the target document type of the iterable.- Parameters:
resultClass
- the class to decode each document into- Returns:
- the list collections iterable interface
- MongoDB documentation
- listCollections
-
listCollectionNames
Gets the names of all the collections in this database.- Parameters:
clientSession
- the client session with which to associate this operation- Returns:
- an iterable containing all the names of all the collections in this database
- Since:
- 3.6
- MongoDB documentation
- listCollections
- Since server release
- 3.6
-
listCollections
Finds all the collections in this database.- Parameters:
clientSession
- the client session with which to associate this operation- Returns:
- the list collections iterable interface
- Since:
- 3.6
- MongoDB documentation
- listCollections
- Since server release
- 3.6
-
listCollections
<TResult> ListCollectionsIterable<TResult> listCollections(ClientSession clientSession, Class<TResult> resultClass) Finds all the collections in this database.- 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 list collections iterable interface
- Since:
- 3.6
- MongoDB documentation
- listCollections
- Since server release
- 3.6
-
createCollection
Create a new collection with the given name.- Parameters:
collectionName
- the name for the new collection to create- MongoDB documentation
- Create Command
-
createCollection
Create a new collection with the selected options- Parameters:
collectionName
- the name for the new collection to createcreateCollectionOptions
- various options for creating the collection- MongoDB documentation
- Create Command
-
createCollection
Create a new collection with the given name.- Parameters:
clientSession
- the client session with which to associate this operationcollectionName
- the name for the new collection to create- Since:
- 3.6
- MongoDB documentation
- Create Command
- Since server release
- 3.6
-
createCollection
void createCollection(ClientSession clientSession, String collectionName, CreateCollectionOptions createCollectionOptions) Create a new collection with the selected options- Parameters:
clientSession
- the client session with which to associate this operationcollectionName
- the name for the new collection to createcreateCollectionOptions
- various options for creating the collection- Since:
- 3.6
- MongoDB documentation
- Create Command
- Since server release
- 3.6
-
createView
Creates a view with the given name, backing collection/view name, and aggregation pipeline that defines the view.- Parameters:
viewName
- the name of the view to createviewOn
- the backing collection/view for the viewpipeline
- the pipeline that defines the view- Since:
- 3.4
- MongoDB documentation
- Create Command
- Since server release
- 3.4
-
createView
void createView(String viewName, String viewOn, List<? extends Bson> pipeline, CreateViewOptions createViewOptions) Creates a view with the given name, backing collection/view name, aggregation pipeline, and options that defines the view.- Parameters:
viewName
- the name of the view to createviewOn
- the backing collection/view for the viewpipeline
- the pipeline that defines the viewcreateViewOptions
- various options for creating the view- Since:
- 3.4
- MongoDB documentation
- Create Command
- Since server release
- 3.4
-
createView
void createView(ClientSession clientSession, String viewName, String viewOn, List<? extends Bson> pipeline) Creates a view with the given name, backing collection/view name, and aggregation pipeline that defines the view.- Parameters:
clientSession
- the client session with which to associate this operationviewName
- the name of the view to createviewOn
- the backing collection/view for the viewpipeline
- the pipeline that defines the view- Since:
- 3.6
- MongoDB documentation
- Create Command
- Since server release
- 3.6
-
createView
void createView(ClientSession clientSession, String viewName, String viewOn, List<? extends Bson> pipeline, CreateViewOptions createViewOptions) Creates a view with the given name, backing collection/view name, aggregation pipeline, and options that defines the view.- Parameters:
clientSession
- the client session with which to associate this operationviewName
- the name of the view to createviewOn
- the backing collection/view for the viewpipeline
- the pipeline that defines the viewcreateViewOptions
- various options for creating the view- Since:
- 3.6
- MongoDB documentation
- Create Command
- Since server release
- 3.6
-
watch
ChangeStreamIterable<Document> watch()Creates a change stream for this database.- Returns:
- the change stream iterable
- Since:
- 3.8
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
Creates a change stream for this database.- 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
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
Creates a change stream for this database.- Parameters:
pipeline
- the aggregation pipeline to apply to the change stream.- Returns:
- the change stream iterable
- Since:
- 3.8
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
<TResult> ChangeStreamIterable<TResult> watch(List<? extends Bson> pipeline, Class<TResult> resultClass) Creates a change stream for this database.- 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
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
Creates a change stream for this database.- Parameters:
clientSession
- the client session with which to associate this operation- Returns:
- the change stream iterable
- Since:
- 3.8
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
<TResult> ChangeStreamIterable<TResult> watch(ClientSession clientSession, Class<TResult> resultClass) Creates a change stream for this database.- 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
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
Creates a change stream for this database.- 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
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
watch
<TResult> ChangeStreamIterable<TResult> watch(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass) Creates a change stream for this database.- 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
- MongoDB documentation
- Change Streams
- Since server release
- 4.0
-
aggregate
Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as$currentOp
and$listLocalSessions
.- Parameters:
pipeline
- the aggregation pipeline- Returns:
- an iterable containing the result of the aggregation operation
- Since:
- 3.10
- MongoDB documentation
- Aggregate Command
- Since server release
- 3.6
-
aggregate
<TResult> AggregateIterable<TResult> aggregate(List<? extends Bson> pipeline, Class<TResult> resultClass) Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as$currentOp
and$listLocalSessions
.- Type Parameters:
TResult
- the target document type of the iterable.- Parameters:
pipeline
- the aggregation pipelineresultClass
- the class to decode each document into- Returns:
- an iterable containing the result of the aggregation operation
- Since:
- 3.10
- MongoDB documentation
- Aggregate Command
- Since server release
- 3.6
-
aggregate
Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as$currentOp
and$listLocalSessions
.- Parameters:
clientSession
- the client session with which to associate this operationpipeline
- the aggregation pipeline- Returns:
- an iterable containing the result of the aggregation operation
- Since:
- 3.10
- MongoDB documentation
- Aggregate Command
- Since server release
- 3.6
-
aggregate
<TResult> AggregateIterable<TResult> aggregate(ClientSession clientSession, List<? extends Bson> pipeline, Class<TResult> resultClass) Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as$currentOp
and$listLocalSessions
.- Type Parameters:
TResult
- the target document type of the iterable.- Parameters:
clientSession
- the client session with which to associate this operationpipeline
- the aggregation pipelineresultClass
- the class to decode each document into- Returns:
- an iterable containing the result of the aggregation operation
- Since:
- 3.10
- MongoDB documentation
- Aggregate Command
- Since server release
- 3.6
-