com.mongodb
Class Mongo

java.lang.Object
  extended by com.mongodb.Mongo
Direct Known Subclasses:
MongoClient

public class Mongo
extends Object

A database connection with internal connection pooling. For most applications, you should have one 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();
 Mongo mongo1 = new Mongo("localhost");
 Mongo mongo2 = new Mongo("localhost", 27017);
 Mongo mongo4 = new Mongo(new ServerAddress("localhost"));
 

You can connect to a replica set using the Java driver by passing a ServerAddress list to the Mongo constructor. For example:

 Mongo mongo = new Mongo(Arrays.asList(
   new ServerAddress("localhost", 27017),
   new ServerAddress("localhost", 27018),
   new ServerAddress("localhost", 27019)));
 
You can connect to a sharded cluster using the same constructor. Mongo will auto-detect whether the servers are a list of replica set members or a list of mongos servers.

By default, all read and write operations will be made on the primary, but it's possible to read from secondaries by changing the read preference:

 mongo.setReadPreference(ReadPreference.secondary());
 
By default, write operations will not throw exceptions on failure, but that is easily changed too:

 mongo.setWriteConcern(WriteConcern.SAFE);
 
Note: This class has been superseded by MongoClient, and may be deprecated in a future release.

See Also:
MongoClient, ReadPreference, WriteConcern

Nested Class Summary
static class Mongo.Holder
          Mongo.Holder can be used as a static place to hold several instances of Mongo.
 
Field Summary
static int MAJOR_VERSION
          Deprecated. Replaced by Mongo.getMajorVersion()
static int MINOR_VERSION
          Deprecated. Replaced by Mongo.getMinorVersion()
 
Constructor Summary
Mongo()
          Creates a Mongo instance based on a (single) mongodb node (localhost, default port)
Mongo(List<ServerAddress> seeds)
          Creates a Mongo based on a list of replica set members or a list of mongos.
Mongo(List<ServerAddress> seeds, MongoOptions options)
          Creates a Mongo based on a list of replica set members or a list of mongos.
Mongo(MongoURI uri)
          Creates a Mongo described by a URI.
Mongo(ServerAddress addr)
          Creates a Mongo instance based on a (single) mongodb node
Mongo(ServerAddress addr, MongoOptions options)
          Creates a Mongo instance based on a (single) mongo node using a given ServerAddress
Mongo(ServerAddress left, ServerAddress right)
          Deprecated. 
Mongo(ServerAddress left, ServerAddress right, MongoOptions options)
          Deprecated. 
Mongo(String host)
          Creates a Mongo instance based on a (single) mongodb node (default port)
Mongo(String host, int port)
          Creates a Mongo instance based on a (single) mongodb node
Mongo(String host, MongoOptions options)
          Creates a Mongo instance based on a (single) mongodb node (default port)
 
Method Summary
 void addOption(int option)
          adds a default query option
 void close()
          closes the underlying connector, which in turn closes all open connections.
static DB connect(DBAddress addr)
          returns a database object
 String debugString()
          returns a string representing the hosts used in this Mongo instance
 void dropDatabase(String dbName)
          Drops the database if it exists.
 CommandResult fsync(boolean async)
          Forces the master server to fsync the RAM data to disk This is done automatically by the server at intervals, but can be forced for better reliability.
 CommandResult fsyncAndLock()
          Forces the master server to fsync the RAM data to disk, then lock all writes.
 ServerAddress getAddress()
          Gets the address of the current master
 List<ServerAddress> getAllAddress()
          Gets a list of all server addresses used when this Mongo was created
 DBTCPConnector getConnector()
          Gets the underlying TCP connector
 String getConnectPoint()
          Gets the current master's hostname
 List<String> getDatabaseNames()
          gets a list of all database names present on the server
 DB getDB(String dbname)
          gets a database object
static int getMajorVersion()
          Gets the major version of this library
 int getMaxBsonObjectSize()
          Gets the maximum size for a BSON object supported by the current master server.
static int getMinorVersion()
          Gets the minor version of this library
 MongoOptions getMongoOptions()
          Returns the mongo options.
 int getOptions()
          gets the default query options
 ReadPreference getReadPreference()
          Gets the default read preference
 ReplicaSetStatus getReplicaSetStatus()
          Gets the replica set status object
 List<ServerAddress> getServerAddressList()
          Gets the list of server addresses currently seen by the connector.
 Collection<DB> getUsedDatabases()
          gets a collection of DBs used by the driver since this Mongo instance was created.
 String getVersion()
          gets this driver version
 WriteConcern getWriteConcern()
          Gets the default write concern
 boolean isLocked()
          Returns true if the database is locked (read-only), false otherwise.
 void resetOptions()
          reset the default query options
 void setOptions(int options)
          sets the default query options
 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()
           
 DBObject unlock()
          Unlocks the database, allowing the write operations to go through.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MAJOR_VERSION

@Deprecated
public static final int MAJOR_VERSION
Deprecated. Replaced by Mongo.getMajorVersion()
See Also:
Constant Field Values

MINOR_VERSION

@Deprecated
public static final int MINOR_VERSION
Deprecated. Replaced by Mongo.getMinorVersion()
See Also:
Constant Field Values
Constructor Detail

Mongo

public Mongo()
      throws UnknownHostException
Creates a Mongo instance based on a (single) mongodb node (localhost, default port)

Throws:
UnknownHostException
MongoException

Mongo

public Mongo(String host)
      throws UnknownHostException
Creates a Mongo instance based on a (single) mongodb node (default port)

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

Mongo

public Mongo(String host,
             MongoOptions options)
      throws UnknownHostException
Creates a Mongo instance based on a (single) mongodb node (default port)

Parameters:
host - server to connect to
options - default query options
Throws:
UnknownHostException - if the database host cannot be resolved
MongoException

Mongo

public Mongo(String host,
             int port)
      throws UnknownHostException
Creates a Mongo instance based on a (single) mongodb node

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

Mongo

public Mongo(ServerAddress addr)
Creates a Mongo instance based on a (single) mongodb node

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

Mongo

public Mongo(ServerAddress addr,
             MongoOptions options)
Creates a Mongo instance based on a (single) mongo node using a given ServerAddress

Parameters:
addr - the database address
options - default query options
Throws:
MongoException
See Also:
ServerAddress

Mongo

@Deprecated
public Mongo(ServerAddress left,
                        ServerAddress right)
Deprecated. 

Creates a Mongo in paired mode.
This will also work for a replica set and will find all members (the master will be used by default).

Parameters:
left - left side of the pair
right - right side of the pair
Throws:
MongoException
See Also:
ServerAddress

Mongo

@Deprecated
public Mongo(ServerAddress left,
                        ServerAddress right,
                        MongoOptions options)
Deprecated. 

Creates a Mongo connection in paired mode.
This will also work for a replica set and will find all members (the master will be used by default).

Parameters:
left - left side of the pair
right - right side of the pair
options -
Throws:
MongoException
See Also:
ServerAddress

Mongo

public Mongo(List<ServerAddress> seeds)
Creates a Mongo based on a list of replica set members or a list of mongos. It will find all members (the master will be used by default). If you pass in a single server in the list, the driver will still function as if it is a replica set. If you have a standalone server, use the Mongo(ServerAddress) constructor.

If this is a list of mongos servers, it will pick the closest (lowest ping time) one to send all requests to, and automatically fail over to the next server if the closest is down.

Parameters:
seeds - Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod servers in the same replica set or a list of mongos servers in the same sharded cluster.
Throws:
MongoException
See Also:
ServerAddress

Mongo

public Mongo(List<ServerAddress> seeds,
             MongoOptions options)
Creates a Mongo based on a list of replica set members or a list of mongos. It will find all members (the master will be used by default). If you pass in a single server in the list, the driver will still function as if it is a replica set. If you have a standalone server, use the Mongo(ServerAddress) constructor.

If this is a list of mongos servers, it will pick the closest (lowest ping time) one to send all requests to, and automatically fail over to the next server if the closest is down.

Parameters:
seeds - Put as many servers as you can in the list and the system will figure out the rest. This can either be a list of mongod servers in the same replica set or a list of mongos servers in the same sharded cluster.
options - for configuring this Mongo instance
Throws:
MongoException
See Also:
ServerAddress

Mongo

public Mongo(MongoURI uri)
      throws UnknownHostException
Creates a Mongo described by a URI. If only one address is used it will only connect to that node, otherwise it will discover all nodes. If the URI contains database credentials, the database will be authenticated lazily on first use with those credentials.

Parameters:
uri -
Throws:
MongoException
UnknownHostException
See Also:

examples:

  • mongodb://localhost
  • mongodb://fred:foobar@localhost/

  • MongoDB Doc Links
    Method Detail

    getMajorVersion

    public static int getMajorVersion()
    Gets the major version of this library

    Returns:
    the major version, e.g. 2

    getMinorVersion

    public static int getMinorVersion()
    Gets the minor version of this library

    Returns:
    the minor version, e.g. 8

    connect

    public static DB connect(DBAddress addr)
    returns a database object

    Parameters:
    addr - the database address
    Returns:
    Throws:
    MongoException

    getDB

    public DB getDB(String dbname)
    gets a database object

    Parameters:
    dbname - the database name
    Returns:

    getUsedDatabases

    public Collection<DB> getUsedDatabases()
    gets a collection of DBs used by the driver since this Mongo instance was created. This may include DBs that exist in the client but not yet on the server.

    Returns:

    getDatabaseNames

    public List<String> getDatabaseNames()
    gets a list of all database names present on the server

    Returns:
    Throws:
    MongoException

    dropDatabase

    public void dropDatabase(String dbName)
    Drops the database if it exists.

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

    getVersion

    public String getVersion()
    gets this driver version

    Returns:
    the full version string of this driver, e.g. "2.8.0"

    debugString

    public String debugString()
    returns a string representing the hosts used in this Mongo instance

    Returns:

    getConnectPoint

    public String getConnectPoint()
    Gets the current master's hostname

    Returns:

    getConnector

    public DBTCPConnector getConnector()
    Gets the underlying TCP connector

    Returns:

    getReplicaSetStatus

    public ReplicaSetStatus getReplicaSetStatus()
    Gets the replica set status object

    Returns:

    getAddress

    public ServerAddress getAddress()
    Gets the address of the current master

    Returns:
    the address

    getAllAddress

    public List<ServerAddress> getAllAddress()
    Gets a list of all server addresses used when this Mongo was created

    Returns:

    getServerAddressList

    public List<ServerAddress> getServerAddressList()
    Gets the list of server addresses currently seen by the connector. This includes addresses auto-discovered from a replica set.

    Returns:
    Throws:
    MongoException

    close

    public void close()
    closes the underlying connector, which in turn closes all open connections. Once called, this Mongo instance can no longer be used.


    setWriteConcern

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

    Parameters:
    concern - write concern to use

    getWriteConcern

    public WriteConcern getWriteConcern()
    Gets the default write concern

    Returns:

    setReadPreference

    public void setReadPreference(ReadPreference preference)
    Sets the read preference for this database. Will be used as default for reads from any collection in any database. See the documentation for ReadPreference for more information.

    Parameters:
    preference - Read Preference to use

    getReadPreference

    public ReadPreference getReadPreference()
    Gets the default read preference

    Returns:

    slaveOk

    @Deprecated
    public void slaveOk()
    Deprecated. Replaced with ReadPreference.secondaryPreferred()

    makes it possible to run read queries on secondary nodes

    See Also:
    ReadPreference.secondaryPreferred()

    addOption

    public void addOption(int option)
    adds a default query option

    Parameters:
    option -

    setOptions

    public void setOptions(int options)
    sets the default query options

    Parameters:
    options -

    resetOptions

    public void resetOptions()
    reset the default query options


    getOptions

    public int getOptions()
    gets the default query options

    Returns:

    getMongoOptions

    public MongoOptions getMongoOptions()
    Returns the mongo options.


    getMaxBsonObjectSize

    public int getMaxBsonObjectSize()
    Gets the maximum size for a BSON object supported by the current master server. Note that this value may change over time depending on which server is master. If the size is not known yet, a request may be sent to the master server

    Returns:
    the maximum size
    Throws:
    MongoException

    fsync

    public CommandResult fsync(boolean async)
    Forces the master server to fsync the RAM data to disk This is done automatically by the server at intervals, but can be forced for better reliability.

    Parameters:
    async - if true, the fsync will be done asynchronously on the server.
    Returns:
    Throws:
    MongoException

    fsyncAndLock

    public CommandResult fsyncAndLock()
    Forces the master server to fsync the RAM data to disk, then lock all writes. The database will be read-only after this command returns.

    Returns:
    Throws:
    MongoException

    unlock

    public DBObject unlock()
    Unlocks the database, allowing the write operations to go through. This command may be asynchronous on the server, which means there may be a small delay before the database becomes writable.

    Returns:
    Throws:
    MongoException

    isLocked

    public boolean isLocked()
    Returns true if the database is locked (read-only), false otherwise.

    Returns:
    Throws:
    MongoException

    toString

    public String toString()
    Overrides:
    toString in class Object