public abstract class DB
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
protected boolean |
_readOnly |
Modifier and Type | Method and Description |
---|---|
void |
addOption(int option)
Adds the give option
|
WriteResult |
addUser(java.lang.String username,
char[] passwd)
Adds a new user for this db
|
WriteResult |
addUser(java.lang.String username,
char[] passwd,
boolean readOnly)
Adds a new user for this db
|
boolean |
authenticate(java.lang.String username,
char[] passwd)
Authenticates to db with the given name and password
|
CommandResult |
authenticateCommand(java.lang.String username,
char[] passwd)
Authenticates to db with the given name and password
|
abstract void |
cleanCursors(boolean force) |
boolean |
collectionExists(java.lang.String collectionName)
Checks to see if a collection by name %lt;name> exists.
|
CommandResult |
command(DBObject cmd)
Executes a database command.
|
CommandResult |
command(DBObject cmd,
int options)
Executes a database command.
|
CommandResult |
command(java.lang.String cmd)
Executes a database command.
|
CommandResult |
command(java.lang.String cmd,
int options)
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 a function on the database.
|
protected abstract DBCollection |
doGetCollection(java.lang.String name)
Returns the collection represented by the string <dbName>.<collectionName>.
|
void |
dropDatabase()
Drops this database.
|
java.lang.Object |
eval(java.lang.String code,
java.lang.Object... args)
|
void |
forceError()
For testing purposes only - this method forces an error to help test error handling
|
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.
|
java.util.Set<java.lang.String> |
getCollectionNames()
Returns a set containing the names of all collections in this database.
|
CommandResult |
getLastError()
Gets the the error (if there is one) from the previous operation on this connection.
|
CommandResult |
getLastError(int w,
int wtimeout,
boolean fsync) |
CommandResult |
getLastError(WriteConcern concern) |
Mongo |
getMongo()
Gets the Mongo instance
|
java.lang.String |
getName()
Returns the name of this database.
|
int |
getOptions()
Gets the query options
|
CommandResult |
getPreviousError()
Returns the last error that occurred since start of database or a call to
resetError()
The return object will look like |
DB |
getSisterDB(java.lang.String name)
Gets another database on same server
|
CommandResult |
getStats()
Returns the result of "dbstats" command
|
WriteConcern |
getWriteConcern()
Gets the write concern for this database.
|
boolean |
isAuthenticated()
Returns true if a user has been authenticated
|
WriteResult |
removeUser(java.lang.String username)
Removes a user for this db
|
abstract void |
requestDone()
ends the current "consistent request"
|
abstract void |
requestEnsureConnection()
ensure that a connection is assigned to the current "consistent request"
|
abstract void |
requestStart()
starts a new "consistent request".
|
void |
resetError()
Resets the error memory for this database.
|
void |
resetOptions()
Resets the query options
|
void |
setOptions(int options)
Sets the query options
|
void |
setReadOnly(java.lang.Boolean b)
Makes this database read-only.
|
void |
setWriteConcern(WriteConcern concern)
Sets the write concern for this database.
|
void |
slaveOk()
Makes it possible to execute "read" queries on a slave node
|
java.lang.String |
toString()
Returns the name of this database.
|
public DB(Mongo mongo, java.lang.String name)
mongo
- the mongo instancename
- the database namepublic abstract void requestStart()
public abstract void requestDone()
public abstract void requestEnsureConnection()
protected abstract DBCollection doGetCollection(java.lang.String name)
name
- the name of the collectionpublic final DBCollection getCollection(java.lang.String name)
name
- the name of the collection to returnpublic final DBCollection createCollection(java.lang.String name, DBObject options)
name
- the name of the collection to returnoptions
- optionspublic DBCollection getCollectionFromString(java.lang.String s)
s
- the name of the collectionpublic CommandResult command(DBObject cmd) throws MongoException
command(com.mongodb.DBObject, int)
with 0 as query option.cmd
- dbobject representing the command to executeMongoException
public CommandResult command(DBObject cmd, int options) throws MongoException
cmd
- dbobject representing the command to executeoptions
- query options to useMongoException
public CommandResult command(java.lang.String cmd) throws MongoException
command(com.mongodb.DBObject)
cmd
- command to executeMongoException
public CommandResult command(java.lang.String cmd, int options) throws MongoException
command(com.mongodb.DBObject, int)
cmd
- command to executeoptions
- query options to useMongoException
public CommandResult doEval(java.lang.String code, java.lang.Object... args) throws MongoException
code
- the function in javascript codeargs
- arguments to be passed to the functionMongoException
public java.lang.Object eval(java.lang.String code, java.lang.Object... args) throws MongoException
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
- the function in javascript codeargs
- arguments to be passed to the functionMongoException
public CommandResult getStats()
public java.lang.String getName()
public void setReadOnly(java.lang.Boolean b)
b
- if the database should be read-onlypublic java.util.Set<java.lang.String> getCollectionNames() throws MongoException
MongoException
public boolean collectionExists(java.lang.String collectionName)
collectionName
- The collection to test for existencepublic java.lang.String toString()
toString
in class java.lang.Object
public CommandResult getLastError() throws MongoException
{ "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()
For most purposes it is better not to call this method directly but instead use WriteConcern
MongoException
public CommandResult getLastError(WriteConcern concern) throws MongoException
concern
- the concern associated with "getLastError" callMongoException
DB#getLastError()}
public CommandResult getLastError(int w, int wtimeout, boolean fsync) throws MongoException
w
- wtimeout
- fsync
- MongoException
DB#getLastError(com.mongodb.WriteConcern) }
public void setWriteConcern(WriteConcern concern)
WriteConcern
for more information.concern
- write concern to usepublic WriteConcern getWriteConcern()
public void dropDatabase() throws MongoException
MongoException
public boolean isAuthenticated()
public boolean authenticate(java.lang.String username, char[] passwd) throws MongoException
username
- name of user for this databasepasswd
- password of user for this databaseMongoException
public CommandResult authenticateCommand(java.lang.String username, char[] passwd) throws MongoException
username
- name of user for this databasepasswd
- password of user for this databaseMongoException
- if authentication failed due to invalid user/pass, or other exceptions like I/Opublic WriteResult addUser(java.lang.String username, char[] passwd)
username
- passwd
- public WriteResult addUser(java.lang.String username, char[] passwd, boolean readOnly)
username
- passwd
- readOnly
- if true, user will only be able to readpublic WriteResult removeUser(java.lang.String username)
username
- public CommandResult getPreviousError() throws MongoException
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.MongoException
public void resetError() throws MongoException
getPreviousError()
will return no error.MongoException
public void forceError() throws MongoException
MongoException
public Mongo getMongo()
public DB getSisterDB(java.lang.String name)
name
- name of the databasepublic void slaveOk()
public void addOption(int option)
option
- public void setOptions(int options)
options
- public void resetOptions()
public int getOptions()
public abstract void cleanCursors(boolean force) throws MongoException
MongoException