com.mongodb
Class DB

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

public abstract class DB
extends Object

a logical database on a server


MongoDB Doc Links

Nested Class Summary
static class DB.WriteConcern
          Settings for strictness of error checking on writes (inserts, updates, and removes).
 
Field Summary
protected  boolean _readOnly
           
 
Constructor Summary
DB(String name)
           
 
Method Summary
 void addUser(String username, char[] passwd)
           
 boolean authenticate(String username, char[] passwd)
          Authenticates connection/db with given name and password
 boolean collectionExists(String collectionName)
          Checks to see if a collection by name %lt;name> exists.
 CommandResult command(DBObject cmd)
          Execute a database command directly.
 CommandResult command(String cmd)
           
 DBCollection createCollection(String name, DBObject o)
          Creates a collection with a given name and options.
 CommandResult doEval(String code, Object... args)
           
protected abstract  DBCollection doGetCollection(String name)
           
 void dropDatabase()
          Drops this database.
 Object eval(String code, Object... args)
           
 void forceError()
          For testing purposes only - this method forces an error to help test error handling
 DBCollection getCollection(String name)
          Gets a collection with a given name.
abstract  DBCollection getCollectionFromFull(String fullNameSpace)
          Returns the collection represented by the string <dbName>.<collectionName>.
 DBCollection getCollectionFromString(String s)
          Returns a collection matching a given string.
 Set<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.
 String getName()
          Returns the name of this database.
 CommandResult getPreviousError()
          Returns the last error that occurred since start of database or a call to resetError() The return object will look like
abstract  DB getSisterDB(String dbName)
           
 DB.WriteConcern getWriteConcern()
          Get the write concern for this database.
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 setReadOnly(Boolean b)
          Makes this database read-only
 void setWriteConcern(DB.WriteConcern concern)
          Set the write concern for this database.
 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(String name)
Method Detail

requestStart

public abstract void requestStart()

requestDone

public abstract void requestDone()

requestEnsureConnection

public abstract void requestEnsureConnection()

getCollectionFromFull

public abstract DBCollection getCollectionFromFull(String fullNameSpace)
Returns the collection represented by the string <dbName>.<collectionName>.

Parameters:
fullNameSpace - the string
Returns:
the collection

doGetCollection

protected abstract DBCollection doGetCollection(String name)

getSisterDB

public abstract DB getSisterDB(String dbName)

getCollection

public final DBCollection getCollection(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(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(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:
Mongo Commands
MongoDB Doc Links

command

public CommandResult command(String cmd)
                      throws MongoException
Throws:
MongoException

doEval

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

eval

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

getName

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

Returns:
the name

setReadOnly

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

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

getCollectionNames

public Set<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(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 String toString()
Returns the name of this database.

Overrides:
toString in class 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 , "_ns" : "$cmd"}
 
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

setWriteConcern

public void setWriteConcern(DB.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 DB.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

authenticate

public boolean authenticate(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(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