com.mongodb
Class DB

java.lang.Object
  extended by com.mongodb.DB
Direct Known Subclasses:
DBApiLayer

public abstract class DB
extends java.lang.Object

a logical database on a server


MongoDB Doc Links

Nested Class Summary
static class DB.WriteConcern
          Deprecated.  
 
Field Summary
protected  boolean _readOnly
           
 
Constructor Summary
DB(Mongo mongo, java.lang.String name)
           
 
Method Summary
 void addOption(int option)
           
 void addUser(java.lang.String username, char[] passwd)
           
 boolean authenticate(java.lang.String username, char[] passwd)
          Authenticates connection/db with given name and password
 boolean collectionExists(java.lang.String collectionName)
          Checks to see if a collection by name %lt;name> exists.
 CommandResult command(DBObject cmd)
          Execute a database command directly.
 CommandResult command(DBObject cmd, int options)
          Execute a database command directly.
 CommandResult command(java.lang.String cmd)
           
 DBCollection createCollection(java.lang.String name, DBObject o)
          Creates a collection with a given name and options.
 CommandResult doEval(java.lang.String code, java.lang.Object... args)
           
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 of the names of collections in this database.
 CommandResult getLastError()
          Gets the the error (if there is one) from the previous operation.
 CommandResult getLastError(int w, int wtimeout, boolean fsync)
           
 CommandResult getLastError(WriteConcern concern)
           
 Mongo getMongo()
           
 java.lang.String getName()
          Returns the name of this database.
 int getOptions()
           
 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)
           
 CommandResult getStats()
           
 WriteConcern getWriteConcern()
          Get the write concern for this database.
 boolean isAuthenticated()
          Returns true iff this DB is authenticated
abstract  void requestDone()
           
abstract  void requestEnsureConnection()
           
abstract  void requestStart()
           
 void resetError()
          Resets the error memory for this database.
 void resetIndexCache()
          Clears any indices that have not yet been applied to the collections in this database.
 void resetOptions()
           
 void setOptions(int options)
           
 void setReadOnly(java.lang.Boolean b)
          Makes this database read-only
 void setWriteConcern(WriteConcern concern)
          Set the write concern for this database.
 void slaveOk()
          makes this query ok to run on a slave node
 java.lang.String toString()
          Returns the name of this database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_readOnly

protected boolean _readOnly
Constructor Detail

DB

public DB(Mongo mongo,
          java.lang.String name)
Method Detail

requestStart

public abstract void requestStart()

requestDone

public abstract void requestDone()

requestEnsureConnection

public abstract void requestEnsureConnection()

doGetCollection

protected abstract DBCollection doGetCollection(java.lang.String name)
Returns the collection represented by the string <dbName>.<collectionName>.

Parameters:
fullNameSpace - the string
Returns:
the collection

getCollection

public final DBCollection getCollection(java.lang.String name)
Gets a collection with a given name. If the collection does not exist, a new collection is created.

Parameters:
name - the name of the collection to return
Returns:
the collection

createCollection

public final DBCollection createCollection(java.lang.String name,
                                           DBObject o)
Creates a collection with a given name and options. If the collection does not exist, a new collection is created. Possible options:
capped
boolean: if the collection is capped
size
int: collection size
max
int: max number of documents

Parameters:
name - the name of the collection to return
o - options
Returns:
the collection

getCollectionFromString

public DBCollection getCollectionFromString(java.lang.String s)
Returns a collection matching a given string.

Parameters:
s - the name of the collection
Returns:
the collection

command

public CommandResult command(DBObject cmd)
                      throws MongoException
Execute a database command directly.

Returns:
the result of the command from the database
Throws:
MongoException
See Also:
List of Commands
MongoDB Doc Links

command

public CommandResult command(DBObject cmd,
                             int options)
                      throws MongoException
Execute a database command directly.

Returns:
the result of the command from the database
Throws:
MongoException
See Also:
List of Commands
MongoDB Doc Links

command

public CommandResult command(java.lang.String cmd)
                      throws MongoException
Throws:
MongoException

doEval

public CommandResult doEval(java.lang.String code,
                            java.lang.Object... args)
                     throws MongoException
Throws:
MongoException

eval

public java.lang.Object eval(java.lang.String code,
                             java.lang.Object... args)
                      throws MongoException
Throws:
MongoException

getStats

public CommandResult getStats()

getName

public java.lang.String getName()
Returns the name of this database.

Returns:
the name

setReadOnly

public void setReadOnly(java.lang.Boolean b)
Makes this database read-only

Parameters:
b - if the database should be read-only

getCollectionNames

public java.util.Set<java.lang.String> getCollectionNames()
                                                   throws MongoException
Returns a set of the names of collections in this database.

Returns:
the names of collections in this database
Throws:
MongoException

collectionExists

public boolean collectionExists(java.lang.String collectionName)
Checks to see if a collection by name %lt;name> exists.

Parameters:
collectionName - The collection to test for existence
Returns:
false if no collection by that name exists, true if a match to an existing collection was found

toString

public java.lang.String toString()
Returns the name of this database.

Overrides:
toString in class java.lang.Object
Returns:
the name

resetIndexCache

public void resetIndexCache()
Clears any indices that have not yet been applied to the collections in this database.


getLastError

public CommandResult getLastError()
                           throws MongoException
Gets the the error (if there is one) from the previous operation. The result of this command will look like
 { "err" :  errorMessage  , "ok" : 1.0 }
 
The value for errorMessage will be null if no error occurred, or a description otherwise. Care must be taken to ensure that calls to getLastError go to the same connection as that of the previous operation. See com.mongodb.Mongo.requestStart for more information.

Returns:
DBObject with error and status information
Throws:
MongoException

getLastError

public CommandResult getLastError(WriteConcern concern)
                           throws MongoException
Throws:
MongoException

getLastError

public CommandResult getLastError(int w,
                                  int wtimeout,
                                  boolean fsync)
                           throws MongoException
Throws:
MongoException

setWriteConcern

public void setWriteConcern(WriteConcern concern)
Set the write concern for this database. Will be used for writes to any collection in this database. See the documentation for DB.WriteConcern for more information.

Parameters:
concern - write concern to use

getWriteConcern

public WriteConcern getWriteConcern()
Get the write concern for this database.


dropDatabase

public void dropDatabase()
                  throws MongoException
Drops this database. Removes all data on disk. Use with caution.

Throws:
MongoException

isAuthenticated

public boolean isAuthenticated()
Returns true iff this DB is authenticated

Returns:
true if authenticated, false otherwise
MongoDB Doc Links

authenticate

public boolean authenticate(java.lang.String username,
                            char[] passwd)
                     throws MongoException
Authenticates connection/db with given name and password

Parameters:
username - name of user for this database
passwd - password of user for this database
Returns:
true if authenticated, false otherwise
Throws:
MongoException
MongoDB Doc Links

addUser

public void addUser(java.lang.String username,
                    char[] passwd)

getPreviousError

public CommandResult getPreviousError()
                               throws MongoException
Returns the last error that occurred since start of database or a call to resetError() The return object will look like
 { err : errorMessage, nPrev : countOpsBack, ok : 1 }
  
The value for errormMessage will be null of no error has ocurred, or the 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 com.mongodb.Mongo.requestStart for more information.

Returns:
DBObject with error and status information
Throws:
MongoException

resetError

public void resetError()
                throws MongoException
Resets the error memory for this database. Used to clear all errors such that getPreviousError() will return no error.

Throws:
MongoException

forceError

public void forceError()
                throws MongoException
For testing purposes only - this method forces an error to help test error handling

Throws:
MongoException

getMongo

public Mongo getMongo()

getSisterDB

public DB getSisterDB(java.lang.String name)

slaveOk

public void slaveOk()
makes this query ok to run on a slave node


addOption

public void addOption(int option)

setOptions

public void setOptions(int options)

resetOptions

public void resetOptions()

getOptions

public int getOptions()