@ThreadSafe public class DB extends java.lang.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,
java.lang.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(java.lang.String userName,
char[] password)
Deprecated.
Use
DB.command to call either the createUser or updateUser command |
WriteResult |
addUser(java.lang.String userName,
char[] password,
boolean readOnly)
Deprecated.
Use
DB.command to call either the createUser or updateUser command |
boolean |
collectionExists(java.lang.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(java.lang.String command)
Executes a database command.
|
CommandResult |
command(java.lang.String command,
ReadPreference readPreference)
Executes a database command.
|
DBCollection |
createCollection(java.lang.String collectionName,
DBObject options)
Creates a collection with a given name and options.
|
DBCollection |
createView(java.lang.String viewName,
java.lang.String viewOn,
java.util.List<? extends DBObject> pipeline)
Creates a view with the given name, backing collection/view name, and aggregation pipeline that defines the view.
|
DBCollection |
createView(java.lang.String viewName,
java.lang.String viewOn,
java.util.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(java.lang.String code,
java.lang.Object... args)
Evaluates JavaScript functions on the database server.
|
protected DBCollection |
doGetCollection(java.lang.String name)
Gets a collection with a given name.
|
void |
dropDatabase()
Drops this database.
|
java.lang.Object |
eval(java.lang.String code,
java.lang.Object... args)
|
DBCollection |
getCollection(java.lang.String name)
Gets a collection with a given name.
|
DBCollection |
getCollectionFromString(java.lang.String collectionName)
Returns a collection matching a given string.
|
java.util.Set<java.lang.String> |
getCollectionNames()
Returns a set containing the names of all collections in this database.
|
Mongo |
getMongo()
Gets the Mongo instance
|
java.lang.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(java.lang.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(java.lang.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() |
java.lang.String |
toString() |
public DB(Mongo mongo, java.lang.String name)
DB
.mongo
- the mongo instancename
- the database name - must not be empty and cannot contain spacespublic 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(java.lang.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 collectionjava.lang.IllegalArgumentException
- if the name is invalidMongoNamespace.checkCollectionNameValidity(String)
public DBCollection getCollection(java.lang.String name)
name
- the name of the collection to returnjava.lang.IllegalArgumentException
- if the name is invalidMongoNamespace.checkCollectionNameValidity(String)
public void dropDatabase()
MongoException
- if the operation failedpublic DBCollection getCollectionFromString(java.lang.String collectionName)
collectionName
- the name of the collectionpublic java.lang.String getName()
public java.util.Set<java.lang.String> getCollectionNames()
MongoException
- if the operation failedpublic DBCollection createCollection(java.lang.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(java.lang.String viewName, java.lang.String viewOn, java.util.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(java.lang.String viewName, java.lang.String viewOn, java.util.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(java.lang.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(java.lang.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(java.lang.String name)
name
- name of the databasepublic boolean collectionExists(java.lang.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(java.lang.String code, java.lang.Object... args)
code
- String
representation of JavaScript functionargs
- arguments to pass to the JavaScript functionMongoException
- if the operation failedpublic java.lang.Object eval(java.lang.String code, java.lang.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(java.lang.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(java.lang.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(java.lang.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()
public java.lang.String toString()
toString
in class java.lang.Object