public class Mongo extends Object
Mongo instances have connection pooling built in - see the requestStart and requestDone methods for more information. http://www.mongodb.org/display/DOCS/Java+Driver+ConcurrencyMongo 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 mongo4 = new Mongo( new ServerAddress( "127.0.0.1") );
You can connect to a replica set using the Java driver by passing several a list if ServerAddress to the Mongo constructor. For example:
Listaddrs = new ArrayList (); addrs.add( new ServerAddress( "127.0.0.1" , 27017 ) ); addrs.add( new ServerAddress( "127.0.0.1" , 27018 ) ); addrs.add( new ServerAddress( "127.0.0.1" , 27019 ) ); Mongo mongo = new Mongo( addrs );
By default, all read and write operations will be made on the master. But it's possible to read from the slave(s) by using slaveOk:
mongo.slaveOk();
Modifier and Type | Class and Description |
---|---|
static class |
Mongo.Holder
Mongo.Holder can be used as a static place to hold several instances of Mongo.
|
Modifier and Type | Field and Description |
---|---|
static int |
MAJOR_VERSION |
static int |
MINOR_VERSION |
Constructor and Description |
---|
Mongo()
Creates a Mongo instance based on a (single) mongodb node (localhost, default port)
|
Mongo(List<ServerAddress> replicaSetSeeds)
Creates a Mongo based on a replica set, or pair.
|
Mongo(List<ServerAddress> replicaSetSeeds,
MongoOptions options)
Creates a Mongo based on a replica set, or pair.
|
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)
|
Modifier and Type | Method and Description |
---|---|
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
|
int |
getMaxBsonObjectSize()
Gets the maximum size for a BSON object supported by the current master server.
|
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.SECONDARY
|
String |
toString() |
DBObject |
unlock()
Unlocks the database, allowing the write operations to go through.
|
public static final int MAJOR_VERSION
public static final int MINOR_VERSION
public Mongo() throws UnknownHostException, MongoException
UnknownHostException
MongoException
public Mongo(String host) throws UnknownHostException, MongoException
host
- server to connect toUnknownHostException
- if the database host cannot be resolvedMongoException
public Mongo(String host, MongoOptions options) throws UnknownHostException, MongoException
host
- server to connect tooptions
- default query optionsUnknownHostException
- if the database host cannot be resolvedMongoException
public Mongo(String host, int port) throws UnknownHostException, MongoException
host
- the database's host addressport
- the port on which the database is runningUnknownHostException
- if the database host cannot be resolvedMongoException
public Mongo(ServerAddress addr) throws MongoException
addr
- the database addressMongoException
ServerAddress
public Mongo(ServerAddress addr, MongoOptions options) throws MongoException
addr
- the database addressoptions
- default query optionsMongoException
ServerAddress
@Deprecated public Mongo(ServerAddress left, ServerAddress right) throws MongoException
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).
left
- left side of the pairright
- right side of the pairMongoException
ServerAddress
@Deprecated public Mongo(ServerAddress left, ServerAddress right, MongoOptions options) throws MongoException
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).
left
- left side of the pairright
- right side of the pairoptions
- MongoException
ServerAddress
public Mongo(List<ServerAddress> replicaSetSeeds) throws MongoException
Creates a Mongo based on a replica set, or pair. 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.
replicaSetSeeds
- Put as many servers as you can in the list and
the system will figure out the rest.MongoException
ServerAddress
public Mongo(List<ServerAddress> replicaSetSeeds, MongoOptions options) throws MongoException
Creates a Mongo based on a replica set, or pair. It will find all members (the master will be used by default).
replicaSetSeeds
- put as many servers as you can in the list.
the system will figure the rest outoptions
- default query optionsMongoException
ServerAddress
public Mongo(MongoURI uri) throws MongoException, UnknownHostException
uri
- MongoException
UnknownHostException
examples:
- mongodb://127.0.0.1
- mongodb://fred:foobar@127.0.0.1/
public static DB connect(DBAddress addr)
addr
- the database addresspublic DB getDB(String dbname)
dbname
- the database namepublic Collection<DB> getUsedDatabases()
public List<String> getDatabaseNames() throws MongoException
MongoException
public void dropDatabase(String dbName) throws MongoException
dbName
- name of database to dropMongoException
public String getVersion()
public String debugString()
public String getConnectPoint()
public DBTCPConnector getConnector()
public ReplicaSetStatus getReplicaSetStatus()
public ServerAddress getAddress()
public List<ServerAddress> getAllAddress()
public List<ServerAddress> getServerAddressList()
public void close()
public void setWriteConcern(WriteConcern concern)
WriteConcern
for more information.concern
- write concern to usepublic WriteConcern getWriteConcern()
public void setReadPreference(ReadPreference preference)
ReadPreference
for more information.preference
- Read Preference to usepublic ReadPreference getReadPreference()
@Deprecated public void slaveOk()
com.mongodb.ReadPreference.SECONDARY
public void addOption(int option)
option
- public void setOptions(int options)
options
- public void resetOptions()
public int getOptions()
public MongoOptions getMongoOptions()
public int getMaxBsonObjectSize()
public CommandResult fsync(boolean async)
async
- if true, the fsync will be done asynchronously on the server.public CommandResult fsyncAndLock()
public DBObject unlock()
public boolean isLocked()