com.mongodb
Class Mongo

java.lang.Object
  extended by com.mongodb.Mongo

public class Mongo
extends java.lang.Object

A database connection with internal pooling. For most application, you should have 1 Mongo instance for the entire JVM. The following are equivalent, and all connect to the local database running on the default port:

 Mongo mongo1 = new Mongo( "127.0.0.1" );
 Mongo mongo2 = new Mongo( "127.0.0.1", 27017 );
 Mongo mongo3 = new Mongo( new DBAddress( "127.0.0.1:27017", "test" ) )
 
Mongo instances have connection pooling built in - see the requestStart and requestDone methods for more information. http://www.mongodb.org/display/DOCS/Java+Driver+Concurrency

Connecting to a Replica Pair

You can connect to a replica pair using the Java driver by passing two DBAddresses to the Mongo constructor. For example:

 DBAddress left = new DBAddress("localhost:27017/test");
 DBAddress right = new DBAddress("localhost:27018/test");

 Mongo mongo = new Mongo(left, right);
 

If the master of a replica pair goes down, there will be a brief lag before the slave becomes master. Thus, your application should be prepared to catch the exceptions that might be thrown in such a case: IllegalArgumentException, MongoException, and MongoException.Network (depending on when the connection drops).

Once the slave becomes master, the driver will begin using that connection as the master connection and the exceptions will stop being thrown.


Field Summary
static int MAJOR_VERSION
           
static int MINOR_VERSION
           
 
Constructor Summary
Mongo()
           
Mongo(java.util.List<ServerAddress> replicaSetSeeds)
          creates a Mongo connection to a replica set
Mongo(java.util.List<ServerAddress> replicaSetSeeds, MongoOptions options)
          creates a Mongo connection to a replica set
Mongo(ServerAddress addr)
          Connects to Mongo using a given DBAddress
Mongo(ServerAddress addr, MongoOptions options)
          Connects to Mongo using a given DBAddress
Mongo(ServerAddress left, ServerAddress right)
          creates a Mongo connection in paired mode
Mongo(ServerAddress left, ServerAddress right, MongoOptions options)
          creates a Mongo connection in paired mode
Mongo(java.lang.String host)
          Connects to the local mongo instance on default port.
Mongo(java.lang.String host, int port)
          Connects to Mongo using a given host, port, and database.
Mongo(java.lang.String host, MongoOptions options)
          Connects to the local mongo instance on default port.
 
Method Summary
 void addOption(int option)
           
 void close()
          closes all open connections this Mongo cannot be re-used
static DB connect(DBAddress addr)
           
 java.lang.String debugString()
           
 void dropDatabase(java.lang.String dbName)
          Drops the database if it exists.
 ServerAddress getAddress()
          Gets the address of this database.
 java.util.List<ServerAddress> getAllAddress()
           
 java.lang.String getConnectPoint()
           
 java.util.List<java.lang.String> getDatabaseNames()
           
 DB getDB(java.lang.String dbname)
           
 int getOptions()
           
static Mongo getStaticMongo(java.lang.String host)
           
static Mongo getStaticMongo(java.lang.String host, MongoOptions options)
           
 java.lang.String getVersion()
           
 WriteConcern getWriteConcern()
          Get the write concern for this database.
 void resetOptions()
           
 void setOptions(int options)
           
 void setWriteConcern(WriteConcern concern)
          Set the write concern for this database.
 void slaveOk()
          makes thisq query ok to run on a slave node
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAJOR_VERSION

public static final int MAJOR_VERSION
See Also:
Constant Field Values

MINOR_VERSION

public static final int MINOR_VERSION
See Also:
Constant Field Values
Constructor Detail

Mongo

public Mongo()
      throws java.net.UnknownHostException,
             MongoException
Throws:
java.net.UnknownHostException
MongoException

Mongo

public Mongo(java.lang.String host)
      throws java.net.UnknownHostException,
             MongoException
Connects to the local mongo instance on default port.

Parameters:
host - server to connect to
Throws:
java.net.UnknownHostException - if the database host cannot be resolved
MongoException

Mongo

public Mongo(java.lang.String host,
             MongoOptions options)
      throws java.net.UnknownHostException,
             MongoException
Connects to the local mongo instance on default port.

Parameters:
host - server to connect to
options - options to use
Throws:
java.net.UnknownHostException - if the database host cannot be resolved
MongoException

Mongo

public Mongo(java.lang.String host,
             int port)
      throws java.net.UnknownHostException,
             MongoException
Connects to Mongo using a given host, port, and database.

Parameters:
host - the database's host address
port - the port on which the database is running
Throws:
java.net.UnknownHostException - if the database host cannot be resolved
MongoException

Mongo

public Mongo(ServerAddress addr)
      throws MongoException
Connects to Mongo using a given DBAddress

Parameters:
addr - the database address
Throws:
MongoException
See Also:
DBAddress

Mongo

public Mongo(ServerAddress addr,
             MongoOptions options)
      throws MongoException
Connects to Mongo using a given DBAddress

Parameters:
addr - the database address
Throws:
MongoException
See Also:
DBAddress

Mongo

public Mongo(ServerAddress left,
             ServerAddress right)
      throws MongoException
creates a Mongo connection in paired mode

Parameters:
left - left side of the pair
right - right side of the pair
Throws:
MongoException

Mongo

public Mongo(ServerAddress left,
             ServerAddress right,
             MongoOptions options)
      throws MongoException
creates a Mongo connection in paired mode

Parameters:
left - left side of the pair
right - right side of the pair
Throws:
MongoException

Mongo

public Mongo(java.util.List<ServerAddress> replicaSetSeeds)
      throws MongoException
creates a Mongo connection to a replica set

Throws:
MongoException

Mongo

public Mongo(java.util.List<ServerAddress> replicaSetSeeds,
             MongoOptions options)
      throws MongoException
creates a Mongo connection to a replica set

Throws:
MongoException
Method Detail

connect

public static DB connect(DBAddress addr)

getStaticMongo

public static Mongo getStaticMongo(java.lang.String host)
                            throws java.net.UnknownHostException,
                                   MongoException
Throws:
java.net.UnknownHostException
MongoException

getStaticMongo

public static Mongo getStaticMongo(java.lang.String host,
                                   MongoOptions options)
                            throws java.net.UnknownHostException,
                                   MongoException
Throws:
java.net.UnknownHostException
MongoException

getDB

public DB getDB(java.lang.String dbname)

getDatabaseNames

public java.util.List<java.lang.String> getDatabaseNames()
                                                  throws MongoException
Throws:
MongoException

dropDatabase

public void dropDatabase(java.lang.String dbName)
                  throws MongoException
Drops the database if it exists.

Parameters:
dbName - name of database to drop
Throws:
MongoException

getVersion

public java.lang.String getVersion()

debugString

public java.lang.String debugString()

getConnectPoint

public java.lang.String getConnectPoint()

getAddress

public ServerAddress getAddress()
Gets the address of this database.

Returns:
the address

getAllAddress

public java.util.List<ServerAddress> getAllAddress()

close

public void close()
closes all open connections this Mongo cannot be re-used


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 WriteConcern for more information.

Parameters:
concern - write concern to use

getWriteConcern

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


slaveOk

public void slaveOk()
makes thisq 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()