public abstract class DB extends 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,
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[] passwd)
Deprecated.
Use
DB.command to call either the addUser or updateUser command |
WriteResult |
addUser(String username,
char[] passwd,
boolean readOnly)
Deprecated.
Use
DB.command to call either the addUser or updateUser command |
boolean |
authenticate(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(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(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(String cmd)
Executes a database command.
|
CommandResult |
command(String cmd,
int options)
Deprecated.
Use
command(String, ReadPreference) instead. This method will be removed in 3.0. |
CommandResult |
command(String cmd,
ReadPreference readPreference)
Executes a database command.
|
DBCollection |
createCollection(String name,
DBObject options)
Creates a collection with a given name and options.
|
CommandResult |
doEval(String code,
Object... args)
Evaluates JavaScript functions on the database server.
|
protected abstract DBCollection |
doGetCollection(String name)
Gets a collection with a given name.
|
void |
dropDatabase()
Drops this database, deleting the associated data files.
|
Object |
eval(String code,
Object... args)
|
void |
forceError()
Deprecated.
The getlasterror command will not be supported in future versions of MongoDB. Use acknowledged writes instead.
|
DBCollection |
getCollection(String name)
Gets a collection with a given name.
|
DBCollection |
getCollectionFromString(String s)
Returns a collection matching a given string.
|
Set<String> |
getCollectionNames()
Returns a set containing all collections in the existing 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 |
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(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(String username)
Deprecated.
Use
DB.command to call the dropUser command |
abstract void |
requestDone()
Ends the current 'consistent request'.
|
abstract void |
requestEnsureConnection()
Ensure that a connection is assigned to the current 'consistent request'
(from primary pool, if connected to a replica set)
|
abstract void |
requestStart()
Starts a new 'consistent request'.
|
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(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 preference)
Sets the read preference for this database.
|
void |
setWriteConcern(WriteConcern concern)
Sets the write concern for this database.
|
void |
slaveOk()
Deprecated.
Replaced with
ReadPreference.secondaryPreferred() |
String |
toString()
Returns the name of this database.
|
@Deprecated protected boolean _readOnly
setReadOnly(Boolean)
public abstract void requestStart()
requestDone()
is called,
all db operations will use the same underlying connection.
This is useful to ensure that operations happen in a certain order with predictable results.public abstract void requestDone()
public abstract void requestEnsureConnection()
protected abstract DBCollection doGetCollection(String name)
name
- the name of the collectionpublic DBCollection getCollection(String name)
name
- the name of the collection to returnpublic DBCollection createCollection(String name, DBObject 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.
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(String s)
s
- the name of the collectionpublic CommandResult command(DBObject cmd)
command(DBObject, int)
} with 0 as query option.cmd
- DBObject
representation of the command to be executedMongoException
public CommandResult command(DBObject cmd, DBEncoder encoder)
command(com.mongodb.DBObject, int, com.mongodb.DBEncoder)
with 0 as query option.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 cmd@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(String cmd)
command(com.mongodb.DBObject)
cmd
- name of the command to be executedMongoException
@Deprecated public CommandResult command(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(String cmd, ReadPreference readPreference)
command(com.mongodb.DBObject, int,
com.mongodb.ReadPreference)
. The only option used by this method was "slave ok", therefore this method has been replaced with
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(String code, Object... args)
code
- @{code String} representation of JavaScript functionargs
- arguments to pass to the JavaScript functionMongoException
public 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
- @{code String} representation of JavaScript functionargs
- arguments to pass to the JavaScript functionMongoException
public CommandResult getStats()
MongoException
public String getName()
@Deprecated public void setReadOnly(Boolean b)
b
- if the database should be read-onlypublic Set<String> getCollectionNames()
MongoException
public 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
public String toString()
@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 timeframe, the getLastError command will return with an error status.fsync
- if true, wait for mongod
to write this data to disk before returning. Defaults to false.DBObject
with error and status informationMongoException
WriteConcern.ACKNOWLEDGED
public void setWriteConcern(WriteConcern concern)
WriteConcern
for more information.concern
- WriteConcern
to usepublic WriteConcern getWriteConcern()
WriteConcern
to be used for write operations, if not specified explicitlypublic void setReadPreference(ReadPreference preference)
ReadPreference
for more information.preference
- 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(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/OIllegalStateException
- if authentication test has already succeeded with different credentialsauthenticateCommand(String, char[])
@Deprecated public CommandResult authenticateCommand(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/OIllegalStateException
- if authentication test has already succeeded with different credentialsauthenticate(String, char[])
@Deprecated public WriteResult addUser(String username, char[] passwd)
DB.command
to call either the addUser or updateUser commandusername
- the user namepasswd
- the passwordMongoException
@Deprecated public WriteResult addUser(String username, char[] passwd, boolean readOnly)
DB.command
to call either the addUser or updateUser commandusername
- the user namepasswd
- the passwordreadOnly
- if true, user will only be able to readMongoException
@Deprecated public WriteResult removeUser(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()
Mongo
instanceMongo
this database belongs topublic DB getSisterDB(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()