Package com.mongodb
Class DB
- java.lang.Object
-
- com.mongodb.DB
-
@ThreadSafe public class DB extends Object
A thread-safe client view of a logical database in a MongoDB cluster. A DB instance can be achieved from aMongoClientinstance using code like:
SeeMongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("<db name>");MongoClient.getDB(String)for further information about the effective deprecation of this class.- See Also:
MongoClient- MongoDB documentation
- Database
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancollectionExists(String collectionName)Checks to see if a collection with a given name exists on a server.CommandResultcommand(DBObject command)Executes a database command.CommandResultcommand(DBObject command, DBEncoder encoder)Executes a database command.CommandResultcommand(DBObject command, ReadPreference readPreference)Executes the command against the database with the given read preference.CommandResultcommand(DBObject command, ReadPreference readPreference, DBEncoder encoder)Executes a database command with the selected readPreference, and encodes the command using the given encoder.CommandResultcommand(String command)Executes a database command.CommandResultcommand(String command, ReadPreference readPreference)Executes a database command.DBCollectioncreateCollection(String collectionName, DBObject options)Creates a collection with a given name and options.DBCollectioncreateView(String viewName, String viewOn, List<? extends DBObject> pipeline)Creates a view with the given name, backing collection/view name, and aggregation pipeline that defines the view.DBCollectioncreateView(String viewName, String viewOn, List<? extends DBObject> pipeline, DBCreateViewOptions options)Creates a view with the given name, backing collection/view name, aggregation pipeline, and options that defines the view.voiddropDatabase()Drops this database.DBCollectiongetCollection(String name)Gets a collection with a given name.Set<String>getCollectionNames()Returns a set containing the names of all collections in this database.MongoClientgetMongoClient()Gets the MongoClient instanceStringgetName()Returns the name of this database.ReadConcerngetReadConcern()Get the read concern for this database.ReadPreferencegetReadPreference()Gets the read preference for this database.DBgetSisterDB(String name)Gets another database on same serverWriteConcerngetWriteConcern()Gets the write concern for this database.voidsetReadConcern(ReadConcern readConcern)Sets the read concern for this database.voidsetReadPreference(ReadPreference readPreference)Sets the read preference for this database.voidsetWriteConcern(WriteConcern writeConcern)Sets the write concern for this database.StringtoString()
-
-
-
Method Detail
-
getMongoClient
public MongoClient getMongoClient()
Gets the MongoClient instance- Returns:
- the MongoClient instance that this database was constructed from
- Throws:
IllegalStateException- if this DB was not created from a MongoClient instance- Since:
- 3.9
-
setReadPreference
public void setReadPreference(ReadPreference readPreference)
Sets the read preference for this database. Will be used as default for read operations from any collection in this database. See the documentation forReadPreferencefor more information.- Parameters:
readPreference-ReadPreferenceto use- MongoDB documentation
- Read Preference
-
setWriteConcern
public void setWriteConcern(WriteConcern writeConcern)
Sets the write concern for this database. It will be used for write operations to any collection in this database. See the documentation forWriteConcernfor more information.- Parameters:
writeConcern-WriteConcernto use- MongoDB documentation
- Write Concern
-
getReadPreference
public ReadPreference getReadPreference()
Gets the read preference for this database.- Returns:
ReadPreferenceto be used for read operations, if not specified explicitly- MongoDB documentation
- Read Preference
-
getWriteConcern
public WriteConcern getWriteConcern()
Gets the write concern for this database.- Returns:
WriteConcernto be used for write operations, if not specified explicitly- MongoDB documentation
- Write Concern
-
setReadConcern
public void setReadConcern(ReadConcern readConcern)
Sets the read concern for this database.- Parameters:
readConcern- the read concern to use for this collection- Since:
- 3.3
- MongoDB documentation
- Read Concern
- Since server release
- 3.2
-
getReadConcern
public ReadConcern getReadConcern()
Get the read concern for this database.- Returns:
- the
ReadConcern - Since:
- 3.3
- MongoDB documentation
- Read Concern
- Since server release
- 3.2
-
getCollection
public DBCollection getCollection(String name)
Gets a collection with a given name.- Parameters:
name- the name of the collection to return- Returns:
- the collection
- Throws:
IllegalArgumentException- if the name is invalid- See Also:
MongoNamespace.checkCollectionNameValidity(String)
-
dropDatabase
public void dropDatabase()
Drops this database. Removes all data on disk. Use with caution.- Throws:
MongoException- if the operation failed- MongoDB documentation
- Drop Database
-
getName
public String getName()
Returns the name of this database.- Returns:
- the name
-
getCollectionNames
public Set<String> getCollectionNames()
Returns a set containing the names of all collections in this database.- Returns:
- the names of collections in this database
- Throws:
MongoException- if the operation failed- MongoDB documentation
- getCollectionNames()
-
createCollection
public DBCollection createCollection(String collectionName, @Nullable DBObject options)
Creates a collection with a given name and options. If the collection already exists, this throws a
CommandFailureException.Possible options:
- capped (
boolean) - Enables a collection cap. False by default. If enabled, you must specify a size parameter. - size (
int) - If capped is true, size specifies a maximum size in bytes for the capped collection. When capped is false, you may use size to preallocate space. - max (
int) - Optional. Specifies a maximum "cap" in number of documents for capped collections. You must also specify size when specifying max.
Note that if the
optionsparameter isnull, the creation will be deferred to when the collection is written to.- Parameters:
collectionName- the name of the collection to returnoptions- options- Returns:
- the collection
- Throws:
MongoCommandException- if the server is unable to create the collectionWriteConcernException- if theWriteConcernspecified on thisDBcould not be satisfiedMongoException- for all other failures- MongoDB documentation
- createCollection()
- capped (
-
createView
public DBCollection createView(String viewName, String viewOn, List<? extends DBObject> 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 createviewOn- the backing collection/view for the viewpipeline- the pipeline that defines the view- Returns:
- the view as a DBCollection
- Throws:
MongoCommandException- if the server is unable to create the collectionWriteConcernException- if theWriteConcernspecified on thisDBcould not be satisfiedMongoException- for all other failures- Since:
- 3.4
- MongoDB documentation
- Create Command
- Since server release
- 3.4
-
createView
public DBCollection createView(String viewName, String viewOn, List<? extends DBObject> pipeline, DBCreateViewOptions options)
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 viewoptions- the options for creating the view- Returns:
- the view as a DBCollection
- Throws:
MongoCommandException- if the server is unable to create the collectionWriteConcernException- if theWriteConcernspecified on thisDBcould not be satisfiedMongoException- for all other failures- Since:
- 3.4
- MongoDB documentation
- Create Command
- Since server release
- 3.4
-
command
public CommandResult command(String command)
Executes a database command. This method constructs a simple DBObject usingcommandas the field name andtrueas its value, and callscommand(DBObject, ReadPreference)with the default read preference for the database.- Parameters:
command- command to execute- Returns:
- result of command from the database
- Throws:
MongoException- if the command failed- MongoDB documentation
- Commands
-
command
public CommandResult command(DBObject command)
Executes a database command. This method callscommand(DBObject, ReadPreference)with the default read preference for the database.- Parameters:
command-DBObjectrepresentation of the command to be executed- Returns:
- result of the command execution
- Throws:
MongoException- if the command failed- MongoDB documentation
- Commands
-
command
public CommandResult command(DBObject command, DBEncoder encoder)
Executes a database command. This method callscommand(DBObject, ReadPreference, DBEncoder)with the default read preference for the database.- Parameters:
command-DBObjectrepresentation of the command to be executedencoder-DBEncoderto be used for command encoding- Returns:
- result of the command execution
- Throws:
MongoException- if the command failed- MongoDB documentation
- Commands
-
command
public CommandResult command(DBObject command, ReadPreference readPreference, @Nullable DBEncoder encoder)
Executes a database command with the selected readPreference, and encodes the command using the given encoder.- Parameters:
command- TheDBObjectrepresentation the command to be executedreadPreference- Where to execute the command - this will only be applied for a subset of commandsencoder- The DBEncoder that knows how to serialise the command- Returns:
- The result of executing the command, success or failure
- Since:
- 2.12
- MongoDB documentation
- Commands
-
command
public CommandResult command(DBObject command, ReadPreference readPreference)
Executes the command against the database with the given read preference.- Parameters:
command- TheDBObjectrepresentation the command to be executedreadPreference- Where to execute the command - this will only be applied for a subset of commands- Returns:
- The result of executing the command, success or failure
- Since:
- 2.12
- MongoDB documentation
- Commands
-
command
public CommandResult command(String command, ReadPreference readPreference)
Executes a database command. This method constructs a simpleDBObjectand callscommand(DBObject, ReadPreference).- Parameters:
command- The name of the command to be executedreadPreference- Where to execute the command - this will only be applied for a subset of commands- Returns:
- The result of the command execution
- Throws:
MongoException- if the command failed- Since:
- 2.12
- MongoDB documentation
- Commands
-
getSisterDB
public DB getSisterDB(String name)
Gets another database on same server- Parameters:
name- name of the database- Returns:
- the DB for the given name
-
collectionExists
public boolean collectionExists(String collectionName)
Checks to see if a collection with a given name exists on a server.- Parameters:
collectionName- a name of the collection to test for existence- Returns:
falseif no collection by that name exists,trueif a match to an existing collection was found- Throws:
MongoException- if the operation failed
-
-