Package com.mongodb
Class DB
-
- com.mongodb.DB
-
@ThreadSafe public class DB extends
A thread-safe client view of a logical database in a MongoDB cluster. A DB instance can be achieved from aMongoClient
instance using code like:MongoClient mongoClient = new MongoClient(); DB db = mongoClient.getDB("<db name>");
Mongo.getDB(String)
for further information about the effective deprecation of this class.- See Also:
MongoClient
- MongoDB documentation
-
-
Constructor Summary
Constructors Constructor Description DB(Mongo mongo, name)
Deprecated.PreferMongo.getDB(String)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addOption(int option)
Deprecated.Replaced withDBCursor.addOption(int)
WriteResult
userName, char[] password)
Deprecated.UseDB.command
to call either the createUser or updateUser commandWriteResult
userName, char[] password, boolean readOnly)
Deprecated.UseDB.command
to call either the createUser or updateUser commandboolean
collectionName)
Checks to see if a collection with a given name exists on a server.CommandResult
command(DBObject command)
Executes a database command.CommandResult
command(DBObject command, DBEncoder encoder)
Executes a database command.CommandResult
command(DBObject command, ReadPreference readPreference)
Executes the command against the database with the given read preference.CommandResult
command(DBObject command, ReadPreference readPreference, DBEncoder encoder)
Executes a database command with the selected readPreference, and encodes the command using the given encoder.CommandResult
command)
Executes a database command.CommandResult
command, ReadPreference readPreference)
Executes a database command.DBCollection
collectionName, DBObject options)
Creates a collection with a given name and options.DBCollection
viewName, viewOn, <? extends DBObject> pipeline)
Creates a view with the given name, backing collection/view name, and aggregation pipeline that defines the view.DBCollection
viewName, viewOn, <? extends DBObject> pipeline, DBCreateViewOptions options)
Creates a view with the given name, backing collection/view name, aggregation pipeline, and options that defines the view.CommandResult
code, ... args)
Deprecated.The eval command was deprecated in MongoDB 3.0protected DBCollection
name)
Deprecated.there is no replacement for this methodvoid
dropDatabase()
Drops this database.code, ... args)
Deprecated.The eval command was deprecated in MongoDB 3.0DBCollection
name)
Gets a collection with a given name.DBCollection
collectionName)
Deprecated.PrefergetCollection(String)
<>
getCollectionNames()
Returns a set containing the names of all collections in this database.Mongo
getMongo()
Deprecated.UsegetMongoClient()
insteadMongoClient
getMongoClient()
Gets the MongoClient instancegetName()
Returns the name of this database.int
getOptions()
Deprecated.Replaced withDBCursor.getOptions()
ReadConcern
getReadConcern()
Get the read concern for this database.ReadPreference
getReadPreference()
Gets the read preference for this database.DB
name)
Gets another database on same serverCommandResult
getStats()
Deprecated.Prefercommand(DBObject)
to run the dbStats commandWriteConcern
getWriteConcern()
Gets the write concern for this database.WriteResult
userName)
Deprecated.UseDB.command
to call the dropUser commandvoid
resetOptions()
Deprecated.Replaced withDBCursor.resetOptions()
void
setOptions(int options)
Deprecated.Replaced withDBCursor.setOptions(int)
void
setReadConcern(ReadConcern readConcern)
Sets the read concern for this database.void
setReadPreference(ReadPreference readPreference)
Sets the read preference for this database.void
setWriteConcern(WriteConcern writeConcern)
Sets the write concern for this database.void
slaveOk()
Deprecated.Replaced withReadPreference.secondaryPreferred()
toString()
-
-
-
Constructor Detail
-
DB
public DB(Mongo mongo, name)
Deprecated.PreferMongo.getDB(String)
Constructs a new instance of theDB
.- Parameters:
mongo
- the mongo instancename
- the database name - must not be empty and cannot contain spaces
-
-
Method Detail
-
getMongo
public Mongo getMongo()
Deprecated.UsegetMongoClient()
insteadGets the Mongo instance- Returns:
- the mongo instance that this database was created from
-
getMongoClient
public MongoClient getMongoClient()
Gets the MongoClient instance- Returns:
- the MongoClient instance that this database was constructed from
- Throws:
- 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 forReadPreference
for more information.- Parameters:
readPreference
-ReadPreference
to use- MongoDB documentation
-
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 forWriteConcern
for more information.- Parameters:
writeConcern
-WriteConcern
to use- MongoDB documentation
-
getReadPreference
public ReadPreference getReadPreference()
Gets the read preference for this database.- Returns:
ReadPreference
to be used for read operations, if not specified explicitly- MongoDB documentation
-
getWriteConcern
public WriteConcern getWriteConcern()
Gets the write concern for this database.- Returns:
WriteConcern
to be used for write operations, if not specified explicitly- MongoDB documentation
-
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
- Since server release
-
getReadConcern
public ReadConcern getReadConcern()
Get the read concern for this database.- Returns:
- the
ReadConcern
- Since:
- 3.3
- MongoDB documentation
- Since server release
-
doGetCollection
protected name)
Deprecated.there is no replacement for this methodGets a collection with a given name. If the collection does not exist, a new collection is created.
This class is NOT part of the public API. Be prepared for non-binary compatible changes in minor releases.
- Parameters:
name
- the name of the collection- Returns:
- the collection
- Throws:
- if the name is invalid
- See Also:
MongoNamespace.checkCollectionNameValidity(String)
-
getCollection
public name)
Gets a collection with a given name.- Parameters:
name
- the name of the collection to return- Returns:
- the collection
- Throws:
- 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
-
getCollectionFromString
public collectionName)
Deprecated.PrefergetCollection(String)
Returns a collection matching a given string.- Parameters:
collectionName
- the name of the collection- Returns:
- the collection
-
getName
public getName()
Returns the name of this database.- Returns:
- the name
-
getCollectionNames
public <> 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
-
createCollection
public 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
options
parameter 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 theWriteConcern
specified on thisDB
could not be satisfiedMongoException
- for all other failures- MongoDB documentation
- capped (
-
createView
public viewName, viewOn, <? 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 theWriteConcern
specified on thisDB
could not be satisfiedMongoException
- for all other failures- Since:
- 3.4
- MongoDB documentation
- Since server release
-
createView
public viewName, viewOn, <? 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 theWriteConcern
specified on thisDB
could not be satisfiedMongoException
- for all other failures- Since:
- 3.4
- MongoDB documentation
- Since server release
-
command
public command)
Executes a database command. This method constructs a simple DBObject usingcommand
as the field name andtrue
as 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
-
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
-DBObject
representation of the command to be executed- Returns:
- result of the command execution
- Throws:
MongoException
- if the command failed- MongoDB documentation
-
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
-DBObject
representation of the command to be executedencoder
-DBEncoder
to be used for command encoding- Returns:
- result of the command execution
- Throws:
MongoException
- if the command failed- MongoDB documentation
-
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
- TheDBObject
representation 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
-
command
public CommandResult command(DBObject command, ReadPreference readPreference)
Executes the command against the database with the given read preference.- Parameters:
command
- TheDBObject
representation 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
-
command
public command, ReadPreference readPreference)
Executes a database command. This method constructs a simpleDBObject
and 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
-
getSisterDB
public name)
Gets another database on same server- Parameters:
name
- name of the database- Returns:
- the DB for the given name
-
collectionExists
public boolean collectionExists( 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:
false
if no collection by that name exists,true
if a match to an existing collection was found- Throws:
MongoException
- if the operation failed
-
doEval
public code, ... args)
Deprecated.The eval command was deprecated in MongoDB 3.0Evaluates JavaScript functions on the database server. This is useful if you need to touch a lot of data lightly, in which case network transfer could be a bottleneck.- Parameters:
code
-String
representation of JavaScript functionargs
- arguments to pass to the JavaScript function- Returns:
- result of the command execution
- Throws:
MongoException
- if the operation failed- MongoDB documentation
-
eval
public eval( code, ... args)
Deprecated.The eval command was deprecated in MongoDB 3.0CallsdoEval(java.lang.String, java.lang.Object[])
. If the command is successful, the "retval" field is extracted and returned. Otherwise an exception is thrown.- Parameters:
code
-String
representation of JavaScript functionargs
- arguments to pass to the JavaScript function- Returns:
- result of the execution
- Throws:
MongoException
- if the operation failed- MongoDB documentation
-
getStats
public CommandResult getStats()
Deprecated.Prefercommand(DBObject)
to run the dbStats commandHelper method for calling a 'dbStats' command. It returns storage statistics for a given database.- Returns:
- result of the execution
- Throws:
MongoException
- if the operation failed- MongoDB documentation
-
addUser
public userName, char[] password)
Deprecated.UseDB.command
to call either the createUser or updateUser commandAdds or updates a user for this databaseNever create or modify users over an insecure network without the use of TLS.
- Parameters:
userName
- the user namepassword
- the password- Returns:
- the result of executing this operation
- Throws:
MongoException
- if the operation failed- MongoDB documentation
-
addUser
public userName, char[] password, boolean readOnly)
Deprecated.UseDB.command
to call either the createUser or updateUser commandAdds or updates a user for this databaseNever create or modify users over an insecure network without the use of TLS.
- Parameters:
userName
- the user namepassword
- the passwordreadOnly
- if true, user will only be able to read- Returns:
- the result of executing this operation
- Throws:
MongoException
- if the operation failed- MongoDB documentation
-
removeUser
public userName)
Deprecated.UseDB.command
to call the dropUser commandRemoves the specified user from the database.- Parameters:
userName
- user to be removed- Returns:
- the result of executing this operation
- Throws:
MongoException
- if the operation failed- MongoDB documentation
-
slaveOk
public void slaveOk()
Deprecated.Replaced withReadPreference.secondaryPreferred()
Makes it possible to execute "read" queries on a slave node- See Also:
ReadPreference.secondaryPreferred()
-
addOption
public void addOption(int option)
Deprecated.Replaced withDBCursor.addOption(int)
Adds the given flag to the default query options.- Parameters:
option
- value to be added- MongoDB documentation
-
setOptions
public void setOptions(int options)
Deprecated.Replaced withDBCursor.setOptions(int)
Sets the query options, overwriting previous value.- Parameters:
options
- bit vector of query options- MongoDB documentation
-
resetOptions
public void resetOptions()
Deprecated.Replaced withDBCursor.resetOptions()
Resets the query options.- MongoDB documentation
-
getOptions
public int getOptions()
Deprecated.Replaced withDBCursor.getOptions()
Gets the query options- Returns:
- bit vector of query options
- MongoDB documentation
-
toString
public toString()
- Overrides:
in class
-
-