Package com.mongodb.client
Interface MongoDatabase
- 
 @ThreadSafe public interface MongoDatabase The MongoDatabase interface.Note: Additions to this interface will not be considered to break binary compatibility. - Since:
- 3.0
 
- 
- 
Method SummaryAll Methods Instance Methods Abstract Methods Modifier and Type Method Description AggregateIterable<Document>aggregate(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$currentOpand$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$currentOpand$listLocalSessions.AggregateIterable<Document>aggregate(List<? extends Bson> pipeline)Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as$currentOpand$listLocalSessions.<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$currentOpand$listLocalSessions.voidcreateCollection(ClientSession clientSession, String collectionName)Create a new collection with the given name.voidcreateCollection(ClientSession clientSession, String collectionName, CreateCollectionOptions createCollectionOptions)Create a new collection with the selected optionsvoidcreateCollection(String collectionName)Create a new collection with the given name.voidcreateCollection(String collectionName, CreateCollectionOptions createCollectionOptions)Create a new collection with the selected optionsvoidcreateView(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.voidcreateView(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.voidcreateView(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.voidcreateView(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.voiddrop()Drops this database.voiddrop(ClientSession clientSession)Drops this database.CodecRegistrygetCodecRegistry()Get the codec registry for the MongoDatabase.MongoCollection<Document>getCollection(String collectionName)Gets a collection.<TDocument>
 MongoCollection<TDocument>getCollection(String collectionName, Class<TDocument> documentClass)Gets a collection, with a specific default document class.StringgetName()Gets the name of the database.ReadConcerngetReadConcern()Get the read concern for the MongoDatabase.ReadPreferencegetReadPreference()Get the read preference for the MongoDatabase.WriteConcerngetWriteConcern()Get the write concern for the MongoDatabase.MongoIterable<String>listCollectionNames()Gets the names of all the collections in this database.MongoIterable<String>listCollectionNames(ClientSession clientSession)Gets the names of all the collections in this database.ListCollectionsIterable<Document>listCollections()Finds all the collections in this database.ListCollectionsIterable<Document>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.DocumentrunCommand(ClientSession clientSession, Bson command)Executes the given command in the context of the current database with a read preference ofReadPreference.primary().DocumentrunCommand(ClientSession clientSession, Bson command, ReadPreference readPreference)Executes the given command in the context of the current database with the given read preference.<TResult> TResultrunCommand(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> TResultrunCommand(ClientSession clientSession, Bson command, Class<TResult> resultClass)Executes the given command in the context of the current database with a read preference ofReadPreference.primary().DocumentrunCommand(Bson command)Executes the given command in the context of the current database with a read preference ofReadPreference.primary().DocumentrunCommand(Bson command, ReadPreference readPreference)Executes the given command in the context of the current database with the given read preference.<TResult> TResultrunCommand(Bson command, ReadPreference readPreference, Class<TResult> resultClass)Executes the given command in the context of the current database with the given read preference.<TResult> TResultrunCommand(Bson command, Class<TResult> resultClass)Executes the given command in the context of the current database with a read preference ofReadPreference.primary().ChangeStreamIterable<Document>watch()Creates a change stream for this database.ChangeStreamIterable<Document>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.ChangeStreamIterable<Document>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>watch(Class<TResult> resultClass)Creates a change stream for this database.ChangeStreamIterable<Document>watch(List<? extends Bson> pipeline)Creates a change stream for this database.<TResult> ChangeStreamIterable<TResult>watch(List<? extends Bson> pipeline, Class<TResult> resultClass)Creates a change stream for this database.MongoDatabasewithCodecRegistry(CodecRegistry codecRegistry)Create a new MongoDatabase instance with a different codec registry.MongoDatabasewithReadConcern(ReadConcern readConcern)Create a new MongoDatabase instance with a different read concern.MongoDatabasewithReadPreference(ReadPreference readPreference)Create a new MongoDatabase instance with a different read preference.MongoDatabasewithWriteConcern(WriteConcern writeConcern)Create a new MongoDatabase instance with a different write concern.
 
- 
- 
- 
Method Detail- 
getNameString getName() Gets the name of the database.- Returns:
- the database name
 
 - 
getCodecRegistryCodecRegistry getCodecRegistry() Get the codec registry for the MongoDatabase.- Returns:
- the CodecRegistry
 
 - 
getReadPreferenceReadPreference getReadPreference() Get the read preference for the MongoDatabase.- Returns:
- the ReadPreference
 
 - 
getWriteConcernWriteConcern getWriteConcern() Get the write concern for the MongoDatabase.- Returns:
- the WriteConcern
 
 - 
getReadConcernReadConcern getReadConcern() Get the read concern for the MongoDatabase.- Returns:
- the ReadConcern
- Since:
- 3.2
- MongoDB documentation
- Read Concern
- Since server release
- 3.2
 
 - 
withCodecRegistryMongoDatabase withCodecRegistry(CodecRegistry codecRegistry) Create a new MongoDatabase instance with a different codec registry.- Parameters:
- codecRegistry- the new- CodecRegistryfor the database
- Returns:
- a new MongoDatabase instance with the different codec registry
 
 - 
withReadPreferenceMongoDatabase withReadPreference(ReadPreference readPreference) Create a new MongoDatabase instance with a different read preference.- Parameters:
- readPreference- the new- ReadPreferencefor the database
- Returns:
- a new MongoDatabase instance with the different readPreference
 
 - 
withWriteConcernMongoDatabase withWriteConcern(WriteConcern writeConcern) Create a new MongoDatabase instance with a different write concern.- Parameters:
- writeConcern- the new- WriteConcernfor the database
- Returns:
- a new MongoDatabase instance with the different writeConcern
 
 - 
withReadConcernMongoDatabase withReadConcern(ReadConcern readConcern) Create a new MongoDatabase instance with a different read concern.- Parameters:
- readConcern- the new- ReadConcernfor the database
- Returns:
- a new MongoDatabase instance with the different ReadConcern
- Since:
- 3.2
- MongoDB documentation
- Read Concern
- Since server release
- 3.2
 
 - 
getCollectionMongoCollection<Document> getCollection(String collectionName) Gets a collection.- Parameters:
- collectionName- the name of the collection to return
- Returns:
- the collection
- Throws:
- IllegalArgumentException- if collectionName is invalid
- See Also:
- MongoNamespace.checkCollectionNameValidity(String)
 
 - 
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 of- Document.
- Parameters:
- collectionName- the name of the collection to return
- documentClass- the default class to cast any documents returned from the database into.
- Returns:
- the collection
 
 - 
runCommandDocument runCommand(Bson command) Executes the given command in the context of the current database with a read preference ofReadPreference.primary().- Parameters:
- command- the command to be run
- Returns:
- the command result
 
 - 
runCommandDocument runCommand(Bson command, ReadPreference readPreference) Executes the given command in the context of the current database with the given read preference.- Parameters:
- command- the command to be run
- readPreference- the- ReadPreferenceto be used when executing the command
- Returns:
- the command result
 
 - 
runCommand<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().- Type Parameters:
- TResult- the type of the class to use instead of- Document.
- Parameters:
- command- the command to be run
- resultClass- 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.- Type Parameters:
- TResult- the type of the class to use instead of- Document.
- Parameters:
- command- the command to be run
- readPreference- the- ReadPreferenceto be used when executing the command
- resultClass- the class to decode each document into
- Returns:
- the command result
 
 - 
runCommandDocument runCommand(ClientSession clientSession, Bson command) Executes the given command in the context of the current database with a read preference ofReadPreference.primary().- Parameters:
- clientSession- the client session with which to associate this operation
- command- the command to be run
- Returns:
- the command result
- Since:
- 3.6
- Since server release
- 3.6
 
 - 
runCommandDocument runCommand(ClientSession clientSession, Bson command, ReadPreference readPreference) Executes the given command in the context of the current database with the given read preference.- Parameters:
- clientSession- the client session with which to associate this operation
- command- the command to be run
- readPreference- the- ReadPreferenceto be used when executing the command
- Returns:
- the command result
- Since:
- 3.6
- Since server release
- 3.6
 
 - 
runCommand<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().- Type Parameters:
- TResult- the type of the class to use instead of- Document.
- Parameters:
- clientSession- the client session with which to associate this operation
- command- the command to be run
- resultClass- 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.- Type Parameters:
- TResult- the type of the class to use instead of- Document.
- Parameters:
- clientSession- the client session with which to associate this operation
- command- the command to be run
- readPreference- the- ReadPreferenceto be used when executing the command
- resultClass- the class to decode each document into
- Returns:
- the command result
- Since:
- 3.6
- Since server release
- 3.6
 
 - 
dropvoid drop() Drops this database.- MongoDB documentation
- Drop database
 
 - 
dropvoid drop(ClientSession clientSession) 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
 
 - 
listCollectionNamesMongoIterable<String> 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
 
 - 
listCollectionsListCollectionsIterable<Document> listCollections() Finds all the collections in this database.- Returns:
- the list collections iterable interface
- MongoDB documentation
- listCollections
 
 - 
listCollections<TResult> ListCollectionsIterable<TResult> listCollections(Class<TResult> resultClass) 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
 
 - 
listCollectionNamesMongoIterable<String> listCollectionNames(ClientSession clientSession) 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
- Since server release
- 3.6
 
 - 
listCollectionsListCollectionsIterable<Document> listCollections(ClientSession clientSession) 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 operation
- resultClass- the class to decode each document into
- Returns:
- the list collections iterable interface
- Since:
- 3.6
- MongoDB documentation
- listCollections
- Since server release
- 3.6
 
 - 
createCollectionvoid createCollection(String collectionName) Create a new collection with the given name.- Parameters:
- collectionName- the name for the new collection to create
- MongoDB documentation
- Create Command
 
 - 
createCollectionvoid createCollection(String collectionName, CreateCollectionOptions createCollectionOptions) Create a new collection with the selected options- Parameters:
- collectionName- the name for the new collection to create
- createCollectionOptions- various options for creating the collection
- MongoDB documentation
- Create Command
 
 - 
createCollectionvoid createCollection(ClientSession clientSession, String collectionName) Create a new collection with the given name.- Parameters:
- clientSession- the client session with which to associate this operation
- collectionName- the name for the new collection to create
- Since:
- 3.6
- MongoDB documentation
- Create Command
- Since server release
- 3.6
 
 - 
createCollectionvoid 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 operation
- collectionName- the name for the new collection to create
- createCollectionOptions- various options for creating the collection
- Since:
- 3.6
- MongoDB documentation
- Create Command
- Since server release
- 3.6
 
 - 
createViewvoid 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.- Parameters:
- viewName- the name of the view to create
- viewOn- the backing collection/view for the view
- pipeline- the pipeline that defines the view
- Since:
- 3.4
- MongoDB documentation
- Create Command
- Since server release
- 3.4
 
 - 
createViewvoid 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 create
- viewOn- the backing collection/view for the view
- pipeline- the pipeline that defines the view
- createViewOptions- various options for creating the view
- Since:
- 3.4
- MongoDB documentation
- Create Command
- Since server release
- 3.4
 
 - 
createViewvoid 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 operation
- viewName- the name of the view to create
- viewOn- the backing collection/view for the view
- pipeline- the pipeline that defines the view
- Since:
- 3.6
- MongoDB documentation
- Create Command
- Since server release
- 3.6
 
 - 
createViewvoid 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 operation
- viewName- the name of the view to create
- viewOn- the backing collection/view for the view
- pipeline- the pipeline that defines the view
- createViewOptions- various options for creating the view
- Since:
- 3.6
- MongoDB documentation
- Create Command
- Since server release
- 3.6
 
 - 
watchChangeStreamIterable<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<TResult> ChangeStreamIterable<TResult> watch(Class<TResult> resultClass) 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
 
 - 
watchChangeStreamIterable<Document> watch(List<? extends Bson> pipeline) 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 stream
- 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
 
 - 
watchChangeStreamIterable<Document> watch(ClientSession clientSession) 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 operation
- 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
 
 - 
watchChangeStreamIterable<Document> watch(ClientSession clientSession, List<? extends Bson> pipeline) Creates a change stream for this database.- Parameters:
- clientSession- the client session with which to associate this operation
- 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(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 operation
- pipeline- the aggregation pipeline to apply to the change stream
- 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
 
 - 
aggregateAggregateIterable<Document> aggregate(List<? extends Bson> pipeline) Runs an aggregation framework pipeline on the database for pipeline stages that do not require an underlying collection, such as$currentOpand$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$currentOpand$listLocalSessions.- Type Parameters:
- TResult- the target document type of the iterable.
- Parameters:
- pipeline- the aggregation pipeline
- resultClass- 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
 
 - 
aggregateAggregateIterable<Document> aggregate(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$currentOpand$listLocalSessions.- Parameters:
- clientSession- the client session with which to associate this operation
- 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(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$currentOpand$listLocalSessions.- Type Parameters:
- TResult- the target document type of the iterable.
- Parameters:
- clientSession- the client session with which to associate this operation
- pipeline- the aggregation pipeline
- resultClass- 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
 
 
- 
 
-