@ThreadSafe public class DB extends Object
MongoClient
instance
using code like:
MongoClient mongoClient = new MongoClient();
DB db = mongoClient.getDB("<db name>");
See Mongo.getDB(String)
for further information about the effective deprecation of this class.MongoClient
Constructor and Description |
---|
DB(Mongo mongo,
String name)
Constructs a new instance of the
DB . |
Modifier and Type | Method and Description |
---|---|
void |
addOption(int option)
Adds the given flag to the default query options.
|
WriteResult |
addUser(String userName,
char[] password)
Deprecated.
Use
DB.command to call either the createUser or updateUser command |
WriteResult |
addUser(String userName,
char[] password,
boolean readOnly)
Deprecated.
Use
DB.command to call either the createUser or updateUser command |
boolean |
collectionExists(String 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(String command)
Executes a database command.
|
CommandResult |
command(String command,
ReadPreference readPreference)
Executes a database command.
|
DBCollection |
createCollection(String collectionName,
DBObject options)
Creates a collection with a given name and options.
|
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.
|
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.
|
CommandResult |
doEval(String code,
Object... args)
Evaluates JavaScript functions on the database server.
|
protected DBCollection |
doGetCollection(String name)
Gets a collection with a given name.
|
void |
dropDatabase()
Drops this database.
|
Object |
eval(String code,
Object... args)
|
DBCollection |
getCollection(String name)
Gets a collection with a given name.
|
DBCollection |
getCollectionFromString(String collectionName)
Returns a collection matching a given string.
|
Set<String> |
getCollectionNames()
Returns a set containing the names of all collections in this database.
|
Mongo |
getMongo()
Gets the Mongo instance
|
String |
getName()
Returns the name of this database.
|
int |
getOptions()
Gets the query options
|
ReadConcern |
getReadConcern()
Get the read concern for this database.
|
ReadPreference |
getReadPreference()
Gets the read preference for this database.
|
DB |
getSisterDB(String name)
Gets another database on same server
|
CommandResult |
getStats()
Helper method for calling a 'dbStats' command.
|
WriteConcern |
getWriteConcern()
Gets the write concern for this database.
|
WriteResult |
removeUser(String userName)
Deprecated.
Use
DB.command to call the dropUser command |
void |
resetOptions()
Resets the query options.
|
void |
setOptions(int options)
Sets the query options, overwriting previous value.
|
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 with
ReadPreference.secondaryPreferred() |
String |
toString() |
public Mongo getMongo()
public void setReadPreference(ReadPreference readPreference)
ReadPreference
for more information.readPreference
- ReadPreference
to usepublic void setWriteConcern(WriteConcern writeConcern)
WriteConcern
for more information.writeConcern
- WriteConcern
to usepublic ReadPreference getReadPreference()
ReadPreference
to be used for read operations, if not specified explicitlypublic WriteConcern getWriteConcern()
WriteConcern
to be used for write operations, if not specified explicitlypublic void setReadConcern(ReadConcern readConcern)
readConcern
- the read concern to use for this collectionpublic ReadConcern getReadConcern()
ReadConcern
protected DBCollection doGetCollection(String name)
Gets 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.
name
- the name of the collectionIllegalArgumentException
- if the name is invalidMongoNamespace.checkCollectionNameValidity(String)
public DBCollection getCollection(String name)
name
- the name of the collection to returnIllegalArgumentException
- if the name is invalidMongoNamespace.checkCollectionNameValidity(String)
public void dropDatabase()
MongoException
- if the operation failedpublic DBCollection getCollectionFromString(String collectionName)
collectionName
- the name of the collectionpublic String getName()
public Set<String> getCollectionNames()
MongoException
- if the operation failedpublic DBCollection createCollection(String collectionName, DBObject options)
Creates a collection with a given name and options. If the collection already exists,
this throws a CommandFailureException
.
Possible options:
boolean
) - Enables a collection cap. False by default. If enabled,
you must specify a size parameter. 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. 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 is null
, the creation will be deferred to when the collection is written
to.
collectionName
- the name of the collection to returnoptions
- optionsMongoCommandException
- if the server is unable to create the collectionWriteConcernException
- if the WriteConcern
specified on this DB
could not be satisfiedMongoException
- for all other failurespublic DBCollection createView(String viewName, String viewOn, List<? extends DBObject> pipeline)
viewName
- the name of the view to createviewOn
- the backing collection/view for the viewpipeline
- the pipeline that defines the viewMongoCommandException
- if the server is unable to create the collectionWriteConcernException
- if the WriteConcern
specified on this DB
could not be satisfiedMongoException
- for all other failurespublic DBCollection createView(String viewName, String viewOn, List<? extends DBObject> pipeline, DBCreateViewOptions options)
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 viewMongoCommandException
- if the server is unable to create the collectionWriteConcernException
- if the WriteConcern
specified on this DB
could not be satisfiedMongoException
- for all other failurespublic CommandResult command(String command)
command
as the field name and true
as its
value, and calls command(DBObject, ReadPreference)
with the default read preference for the database.command
- command to executeMongoException
- if the command failedpublic CommandResult command(DBObject command)
command(DBObject, ReadPreference)
with the default read preference for the
database.command
- DBObject
representation of the command to be executedMongoException
- if the command failedpublic CommandResult command(DBObject command, DBEncoder encoder)
command(DBObject, ReadPreference, DBEncoder)
with the default read
preference for the database.command
- DBObject
representation of the command to be executedencoder
- DBEncoder
to be used for command encodingMongoException
- if the command failedpublic CommandResult command(DBObject command, ReadPreference readPreference, DBEncoder encoder)
command
- The DBObject
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 commandpublic CommandResult command(DBObject command, ReadPreference readPreference)
command
- The DBObject
representation the command to be executedreadPreference
- Where to execute the command - this will only be applied for a subset of commandspublic CommandResult command(String command, ReadPreference readPreference)
DBObject
and calls command(DBObject, ReadPreference)
.command
- The name of the command to be executedreadPreference
- Where to execute the command - this will only be applied for a subset of commandsMongoException
- if the command failedpublic DB getSisterDB(String name)
name
- name of the databasepublic boolean collectionExists(String collectionName)
collectionName
- a name of the collection to test for existencefalse
if no collection by that name exists, true
if a match to an existing collection was foundMongoException
- if the operation failedpublic CommandResult doEval(String code, Object... args)
code
- String
representation of JavaScript functionargs
- arguments to pass to the JavaScript functionMongoException
- if the operation failedpublic Object eval(String code, Object... args)
doEval(java.lang.String, java.lang.Object[])
. If the command is successful, the "retval" field is extracted and
returned. Otherwise an exception is thrown.code
- String
representation of JavaScript functionargs
- arguments to pass to the JavaScript functionMongoException
- if the operation failedpublic CommandResult getStats()
MongoException
- if the operation failed@Deprecated public WriteResult addUser(String userName, char[] password)
DB.command
to call either the createUser or updateUser commanduserName
- the user namepassword
- the passwordMongoException
- if the operation failed@Deprecated public WriteResult addUser(String userName, char[] password, boolean readOnly)
DB.command
to call either the createUser or updateUser commanduserName
- the user namepassword
- the passwordreadOnly
- if true, user will only be able to readMongoException
- if the operation failed@Deprecated public WriteResult removeUser(String userName)
DB.command
to call the dropUser commanduserName
- user to be removedMongoException
- if the operation failed@Deprecated public void slaveOk()
ReadPreference.secondaryPreferred()
ReadPreference.secondaryPreferred()
public void addOption(int option)
option
- value to be addedpublic void setOptions(int options)
options
- bit vector of query optionspublic void resetOptions()
public int getOptions()