public abstract class DB
extends java.lang.Object
MongoClient
instance
using code like:
MongoClient mongoClient = new MongoClient();
DB db = mongoClient.getDB("<db name>");
MongoClient
Modifier and Type | Field and Description |
---|---|
protected boolean |
_readOnly
Deprecated.
|
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[] passwd)
Deprecated.
Use
DB.command to call either the createUser or updateUser command |
WriteResult |
addUser(java.lang.String username,
char[] passwd,
boolean readOnly)
Deprecated.
Use
DB.command to call either the createUser or updateUser command |
boolean |
authenticate(java.lang.String username,
char[] password)
Deprecated.
Please use
MongoClient.MongoClient(java.util.List, java.util.List) to create a client, which
will authenticate all connections to server |
CommandResult |
authenticateCommand(java.lang.String username,
char[] password)
Deprecated.
Please use
MongoClient.MongoClient(java.util.List, java.util.List) to create a client, which
will authenticate all connections to server |
abstract void |
cleanCursors(boolean force)
Deprecated.
Clients should ensure that
DBCursor.close() is called. |
boolean |
collectionExists(java.lang.String collectionName)
Checks to see if a collection with a given name exists on a server.
|
CommandResult |
command(DBObject cmd)
Executes a database command.
|
CommandResult |
command(DBObject cmd,
DBEncoder encoder)
Executes a database command.
|
CommandResult |
command(DBObject cmd,
int options)
Deprecated.
Use
command(DBObject, ReadPreference) instead. This method will be removed in 3.0. |
CommandResult |
command(DBObject cmd,
int options,
DBEncoder encoder)
Deprecated.
Use
command(DBObject, ReadPreference, DBEncoder) instead. This method will be removed in 3.0. |
CommandResult |
command(DBObject cmd,
int options,
ReadPreference readPreference)
Deprecated.
Use
command(DBObject, ReadPreference) instead. This method will be removed in 3.0. |
CommandResult |
command(DBObject cmd,
int options,
ReadPreference readPreference,
DBEncoder encoder)
Deprecated.
Use
command(DBObject, ReadPreference, DBEncoder) instead. This method will be removed in 3.0. |
CommandResult |
command(DBObject cmd,
ReadPreference readPreference)
Executes the command against the database with the given read preference.
|
CommandResult |
command(DBObject cmd,
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 cmd)
Executes a database command.
|
CommandResult |
command(java.lang.String cmd,
int options)
Deprecated.
Use
command(String, ReadPreference) instead. This method will be removed in 3.0. |
CommandResult |
command(java.lang.String cmd,
ReadPreference readPreference)
Executes a database command.
|
DBCollection |
createCollection(java.lang.String name,
DBObject options)
Creates a collection with a given name and options.
|
CommandResult |
doEval(java.lang.String code,
java.lang.Object... args)
Evaluates JavaScript functions on the database server.
|
protected abstract 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)
|
void |
forceError()
Deprecated.
The getlasterror command will not be supported in future versions of MongoDB. Use acknowledged writes instead.
|
DBCollection |
getCollection(java.lang.String name)
Gets a collection with a given name.
|
DBCollection |
getCollectionFromString(java.lang.String s)
Returns a collection matching a given string.
|
abstract java.util.Set<java.lang.String> |
getCollectionNames()
Returns a set containing the names of all collections in this database.
|
CommandResult |
getLastError()
Deprecated.
The getlasterror command will not be supported in future versions of MongoDB. Use acknowledged writes instead.
|
CommandResult |
getLastError(int w,
int wtimeout,
boolean fsync)
Deprecated.
The getlasterror command will not be supported in future versions of MongoDB. Use acknowledged writes instead.
|
CommandResult |
getLastError(WriteConcern concern)
Deprecated.
The getlasterror command will not be supported in future versions of MongoDB. Use acknowledged writes instead.
|
Mongo |
getMongo()
Gets the Mongo instance
|
java.lang.String |
getName()
Returns the name of this database.
|
int |
getOptions()
Gets the default query options
|
CommandResult |
getPreviousError()
Deprecated.
The getlasterror command will not be supported in future versions of MongoDB. Use acknowledged writes instead.
|
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.
|
boolean |
isAuthenticated()
Deprecated.
Please use
MongoClient.MongoClient(java.util.List, java.util.List) to create a client, which
will authenticate all connections to server |
WriteResult |
removeUser(java.lang.String username)
Deprecated.
Use
DB.command to call the dropUser command |
abstract void |
requestDone()
Deprecated.
The main use case for this method is to ensure that applications can read their own unacknowledged writes,
but this is no longer so prevalent since the driver started defaulting to acknowledged writes. The other main use case is to
ensure that related read operations are all routed to the same server when using a non-primary read preference. But this is
dangerous because mongos does not provide this guarantee. For these reasons, this method is now deprecated and will be
removed in the next major release.
|
abstract void |
requestEnsureConnection()
Deprecated.
The main use case for this method is to ensure that applications can read their own unacknowledged writes,
but this is no longer so prevalent since the driver started defaulting to acknowledged writes. The other main use case is to
ensure that related read operations are all routed to the same server when using a non-primary read preference. But this is
dangerous because mongos does not provide this guarantee. For these reasons, this method is now deprecated and will be
removed in the next major release.
|
abstract void |
requestStart()
Deprecated.
The main use case for this method is to ensure that applications can read their own unacknowledged writes,
but this is no longer so prevalent since the driver started defaulting to acknowledged writes. The other main use case is to
ensure that related read operations are all routed to the same server when using a non-primary read preference. But this is
dangerous because mongos does not provide this guarantee. For these reasons, this method is now deprecated and will be
removed in the next major release.
|
void |
resetError()
Deprecated.
The getlasterror command will not be supported in future versions of MongoDB. Use acknowledged writes instead.
|
void |
resetOptions()
Resets the query options.
|
void |
setOptions(int options)
Sets the default query options, overwriting previous value.
|
void |
setReadOnly(java.lang.Boolean b)
Deprecated.
Avoid making database read-only via this method.
Connect with a user credentials that has a read-only access to a server instead.
|
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()
Returns the name of this database.
|
@Deprecated protected boolean _readOnly
setReadOnly(Boolean)
public DB(Mongo mongo, java.lang.String name)
DB
.mongo
- the mongo instancename
- the database name - must not be empty and cannot contain spaces@Deprecated public abstract void requestStart()
Starts a new 'consistent request'
.Following this call and until requestDone()
is called, all db operations will use the same underlying
connection.
@Deprecated public abstract void requestDone()
@Deprecated public abstract void requestEnsureConnection()
protected abstract 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 collectionpublic DBCollection getCollection(java.lang.String name)
name
- the name of the collection to returnpublic DBCollection createCollection(java.lang.String name, 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.
name
- the name of the collection to returnoptions
- optionsMongoException
public DBCollection getCollectionFromString(java.lang.String s)
s
- the name of the collectionpublic CommandResult command(DBObject cmd)
command(DBObject, ReadPreference)
with the default read preference for the
database.cmd
- DBObject
representation of the command to be executedMongoException
public CommandResult command(DBObject cmd, DBEncoder encoder)
command(DBObject, ReadPreference, DBEncoder)
with the default read
preference for the database.cmd
- DBObject
representation of the command to be executedencoder
- DBEncoder
to be used for command encodingMongoException
@Deprecated public CommandResult command(DBObject cmd, int options, DBEncoder encoder)
command(DBObject, ReadPreference, DBEncoder)
instead. This method will be removed in 3.0.command(com.mongodb.DBObject, int, com.mongodb.ReadPreference, com.mongodb.DBEncoder)
with the database default read
preference. The only option used by this method was "slave ok", therefore this method has been replaced with
command(DBObject, ReadPreference, DBEncoder)
.cmd
- DBObject
representation the command to be executedoptions
- query options to useencoder
- DBEncoder
to be used for command encodingMongoException
@Deprecated public CommandResult command(DBObject cmd, int options, ReadPreference readPreference)
command(DBObject, ReadPreference)
instead. This method will be removed in 3.0.command(com.mongodb.DBObject, int, com.mongodb.ReadPreference, com.mongodb.DBEncoder)
with a default encoder. The only
option used by this method was "slave ok", therefore this method has been replaced
with command(DBObject, ReadPreference)
.cmd
- A DBObject
representation the command to be executedoptions
- The query options to usereadPreference
- The ReadPreference
for this command (nodes selection is the biggest part of this)MongoException
@Deprecated public CommandResult command(DBObject cmd, int options, ReadPreference readPreference, DBEncoder encoder)
command(DBObject, ReadPreference, DBEncoder)
instead. This method will be removed in 3.0.command(DBObject, ReadPreference, DBEncoder)
.cmd
- A DBObject
representation the command to be executedoptions
- The query options to usereadPreference
- The ReadPreference
for this command (nodes selection is the biggest part of this)encoder
- A DBEncoder
to be used for command encodingMongoException
public CommandResult command(DBObject cmd, ReadPreference readPreference, DBEncoder encoder)
cmd
- 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 command@Deprecated public CommandResult command(DBObject cmd, int options)
command(DBObject, ReadPreference)
instead. This method will be removed in 3.0.command(DBObject, ReadPreference)
.cmd
- The DBObject
representation the command to be executedoptions
- The query options to useMongoException
public CommandResult command(DBObject cmd, ReadPreference readPreference)
command(com.mongodb.DBObject, int)
cmd
- 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 cmd)
command
as the field name and true
as its
value, and calls command(DBObject, ReadPreference)
with the default read preference for the database.cmd
- command to executeMongoException
@Deprecated public CommandResult command(java.lang.String cmd, int options)
command(String, ReadPreference)
instead. This method will be removed in 3.0.command(com.mongodb.DBObject, int)
cmd
- name of the command to be executedoptions
- query options to useMongoException
public CommandResult command(java.lang.String cmd, ReadPreference readPreference)
DBObject
and calls command(DBObject, ReadPreference)
.cmd
- The name of the command to be executedreadPreference
- Where to execute the command - this will only be applied for a subset of commandsMongoException
public CommandResult doEval(java.lang.String code, java.lang.Object... args)
code
- String
representation of JavaScript functionargs
- arguments to pass to the JavaScript functionMongoException
public 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
public CommandResult getStats()
MongoException
public java.lang.String getName()
@Deprecated public void setReadOnly(java.lang.Boolean b)
b
- if the database should be read-onlypublic abstract java.util.Set<java.lang.String> getCollectionNames()
MongoException
public 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
public java.lang.String toString()
toString
in class java.lang.Object
@Deprecated public CommandResult getLastError()
{ "err" : errorMessage , "ok" : 1.0 }
The value for errorMessage will be null if no error occurred, or a description otherwise.
Important note: when calling this method directly, it is undefined which connection "getLastError" is called on. You may need
to explicitly use a "consistent Request", see requestStart()
It is better not to call this method directly but instead
use WriteConcern
DBObject
with error and status informationMongoException
WriteConcern.ACKNOWLEDGED
@Deprecated public CommandResult getLastError(WriteConcern concern)
concern
- a WriteConcern
to be used while checking for the error status.DBObject
with error and status informationMongoException
WriteConcern.ACKNOWLEDGED
@Deprecated public CommandResult getLastError(int w, int wtimeout, boolean fsync)
w
- when running with replication, this is the number of servers to replicate to before returning. A w
value
of 1
indicates the primary only. A w
value of 2
includes the primary and at least one
secondary, etc. In place of a number, you may also set w
to majority to indicate that the command should
wait until the latest write propagates to a majority of replica set members. If using w
,
you should also use wtimeout
. Specifying a value for w
without also providing a wtimeout
may
cause getLastError
to block indefinitely.wtimeout
- a value in milliseconds that controls how long to wait for write propagation to complete. If replication does not
complete in the given time frame, the getLastError command will return with an error status.fsync
- if true
, wait for MongoDB to write this data to disk before returning. Defaults to false
.DBObject
with error and status informationMongoException
WriteConcern.ACKNOWLEDGED
public void setWriteConcern(WriteConcern writeConcern)
WriteConcern
for more information.writeConcern
- WriteConcern
to usepublic WriteConcern getWriteConcern()
WriteConcern
to be used for write operations, if not specified explicitlypublic void setReadPreference(ReadPreference readPreference)
ReadPreference
for more information.readPreference
- ReadPreference
to usepublic ReadPreference getReadPreference()
ReadPreference
to be used for read operations, if not specified explicitlypublic void dropDatabase()
MongoException
@Deprecated public boolean isAuthenticated()
MongoClient.MongoClient(java.util.List, java.util.List)
to create a client, which
will authenticate all connections to servertrue
if a user has been authenticated on this database.true
if authenticated, false
otherwise@Deprecated public boolean authenticate(java.lang.String username, char[] password)
MongoClient.MongoClient(java.util.List, java.util.List)
to create a client, which
will authenticate all connections to serverauthenticateCommand
) has already been
called with the same credentials and the authentication test succeeded, this method will return true
. If this method
has already been called with different credentials and the authentication test succeeded,
this method will throw an IllegalStateException
. If this method has already been called with any credentials
and the authentication test failed, this method will re-try the authentication test with the
given credentials.username
- name of user for this databasepassword
- password of user for this databaseMongoException
- if authentication failed due to invalid user/pass, or other exceptions like I/Ojava.lang.IllegalStateException
- if authentication test has already succeeded with different credentialsauthenticateCommand(String, char[])
@Deprecated public CommandResult authenticateCommand(java.lang.String username, char[] password)
MongoClient.MongoClient(java.util.List, java.util.List)
to create a client, which
will authenticate all connections to serverauthenticate
) has already been
called with the same credentials and the authentication test succeeded, this method will return true. If this method
has already been called with different credentials and the authentication test succeeded,
this method will throw an IllegalStateException
. If this method has already been called with any credentials
and the authentication test failed, this method will re-try the authentication test with the
given credentials.username
- name of user for this databasepassword
- password of user for this databaseMongoException
- if authentication failed due to invalid user/pass, or other exceptions like I/Ojava.lang.IllegalStateException
- if authentication test has already succeeded with different credentialsauthenticate(String, char[])
@Deprecated public WriteResult addUser(java.lang.String username, char[] passwd)
DB.command
to call either the createUser or updateUser commandusername
- the user namepasswd
- the passwordMongoException
@Deprecated public WriteResult addUser(java.lang.String username, char[] passwd, boolean readOnly)
DB.command
to call either the createUser or updateUser commandusername
- the user namepasswd
- the passwordreadOnly
- if true, user will only be able to readMongoException
@Deprecated public WriteResult removeUser(java.lang.String username)
DB.command
to call the dropUser commandusername
- user to be removedMongoException
@Deprecated public CommandResult getPreviousError()
resetError()
The return object
will look like:
{ err : errorMessage, nPrev : countOpsBack, ok : 1 }
The value for errorMessage will be null of no error has occurred, otherwise the error message.
The value of countOpsBack will be the number of operations since the error occurred.
Care must be taken to ensure that calls to getPreviousError go to the same connection as that
of the previous operation. See requestStart()
for more information.
DBObject
with error and status informationMongoException
WriteConcern.ACKNOWLEDGED
@Deprecated public void resetError()
getPreviousError()
will return no error.MongoException
WriteConcern.ACKNOWLEDGED
@Deprecated public void forceError()
MongoException
WriteConcern.ACKNOWLEDGED
public Mongo getMongo()
public DB getSisterDB(java.lang.String name)
name
- name of the database@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()
@Deprecated public abstract void cleanCursors(boolean force)
DBCursor.close()
is called.DBCursor.close
force
- true if should clean regardless of number of dead cursorsDBCursor.close()