public class Mongo extends Object
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.MongoClient
,
ReadPreference
,
WriteConcern
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
Deprecated.
Replaced by
getMajorVersion() |
static int |
MINOR_VERSION
Deprecated.
Replaced by
getMinorVersion() |
Constructor and Description |
---|
Mongo()
Deprecated.
Replaced by
MongoClient.MongoClient() ) |
Mongo(List<ServerAddress> seeds)
Deprecated.
Replaced by
MongoClient.MongoClient(java.util.List) |
Mongo(List<ServerAddress> seeds,
MongoOptions options)
Deprecated.
|
Mongo(MongoURI uri)
Deprecated.
Replaced by
MongoClient.MongoClient(MongoClientURI) |
Mongo(ServerAddress addr)
Deprecated.
Replaced by
MongoClient.MongoClient(ServerAddress) |
Mongo(ServerAddress addr,
MongoOptions options)
Deprecated.
|
Mongo(ServerAddress left,
ServerAddress right)
Deprecated.
|
Mongo(ServerAddress left,
ServerAddress right,
MongoOptions options)
Deprecated.
Please use
MongoClient.MongoClient(java.util.List, MongoClientOptions) instead. |
Mongo(String host)
Deprecated.
Replaced by
MongoClient.MongoClient(String) |
Mongo(String host,
int port)
Deprecated.
Replaced by
MongoClient.MongoClient(String, int) |
Mongo(String host,
MongoOptions options)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
void |
addOption(int option)
Add a default query option keeping any previously added options.
|
void |
close()
Closes the underlying connector, which in turn closes all open connections.
|
static DB |
connect(DBAddress addr)
Deprecated.
Please use
getDB(String) instead. |
String |
debugString()
Deprecated.
This method is NOT a part of public API and will be dropped in 3.x versions.
|
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()
Deprecated.
DBTCPConnector is NOT part of the public API. It will be dropped in 3.x releases. |
String |
getConnectPoint()
Gets a
String representation of current connection point, i.e. |
List<String> |
getDatabaseNames()
Gets a list of the names of all databases on the connected server.
|
DB |
getDB(String dbname)
Gets a database object from this MongoDB instance.
|
static int |
getMajorVersion()
Deprecated.
Please use
getVersion() instead. |
int |
getMaxBsonObjectSize()
Gets the maximum size for a BSON object supported by the current master server.
|
static int |
getMinorVersion()
Deprecated.
Please use
getVersion() instead. |
MongoOptions |
getMongoOptions()
Deprecated.
Please use
MongoClient and corresponding MongoClient.getMongoClientOptions() |
int |
getOptions()
Gets the default query options
|
ReadPreference |
getReadPreference()
Gets the default read preference
|
ReplicaSetStatus |
getReplicaSetStatus()
Get the status of the replica set cluster.
|
List<ServerAddress> |
getServerAddressList()
Gets the list of server addresses currently seen by this client.
|
Collection<DB> |
getUsedDatabases()
Returns the list of databases used by the driver since this Mongo instance was created.
|
String |
getVersion()
Deprecated.
There is no replacement for this method.
|
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)
Set 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.
|
@Deprecated public static final int MAJOR_VERSION
getMajorVersion()
@Deprecated public static final int MINOR_VERSION
getMinorVersion()
@Deprecated public Mongo() throws UnknownHostException
MongoClient.MongoClient()
)UnknownHostException
- This exception is no longer thrown, but leaving in throws clause so as not to break source
compatibility. The exception will be removed from the declaration in the next major release of the
driver.@Deprecated public Mongo(String host) throws UnknownHostException
MongoClient.MongoClient(String)
host
- server to connect toUnknownHostException
- This exception is no longer thrown, but leaving in throws clause so as not to break source
compatibility. The exception will be removed from the declaration in the next major release of the
driver.@Deprecated public Mongo(String host, MongoOptions options) throws UnknownHostException
MongoClient.MongoClient(String, MongoClientOptions)
host
- server to connect tooptions
- default query optionsUnknownHostException
- This exception is no longer thrown, but leaving in throws clause so as not to break source
compatibility. The exception will be removed from the declaration in the next major release of the
driver.@Deprecated public Mongo(String host, int port) throws UnknownHostException
MongoClient.MongoClient(String, int)
host
- the database's host addressport
- the port on which the database is runningUnknownHostException
- This exception is no longer thrown, but leaving in throws clause so as not to break source
compatibility. The exception will be removed from the declaration in the next major release of the
driver.@Deprecated public Mongo(ServerAddress addr)
MongoClient.MongoClient(ServerAddress)
addr
- the database addressServerAddress
@Deprecated public Mongo(ServerAddress addr, MongoOptions options)
MongoClient.MongoClient(ServerAddress, MongoClientOptions)
addr
- the database addressoptions
- default query optionsServerAddress
@Deprecated public Mongo(ServerAddress left, ServerAddress right)
left
- left side of the pairright
- right side of the pairMongoException
@Deprecated public Mongo(ServerAddress left, ServerAddress right, MongoOptions options)
MongoClient.MongoClient(java.util.List, MongoClientOptions)
instead.left
- left side of the pairright
- right side of the pairoptions
- the optional settings for the Mongo instanceServerAddress
@Deprecated public Mongo(List<ServerAddress> seeds)
MongoClient.MongoClient(java.util.List)
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.
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.ServerAddress
@Deprecated public Mongo(List<ServerAddress> seeds, MongoOptions options)
MongoClient.MongoClient(java.util.List, MongoClientOptions)
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.
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 instanceServerAddress
@Deprecated public Mongo(MongoURI uri) throws UnknownHostException
MongoClient.MongoClient(MongoClientURI)
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.
Examples:
uri
- URI to connect to, optionally containing additional information like credentialsUnknownHostException
MongoURI
@Deprecated public static int getMajorVersion()
getVersion()
instead.@Deprecated public static int getMinorVersion()
getVersion()
instead.@Deprecated public static DB connect(DBAddress addr)
getDB(String)
instead.DB
specified in the DBAddress
parameter.addr
- The details of the server and database to connect toMongoException
public DB getDB(String dbname)
dbname
- the name of the database to retrievepublic Collection<DB> getUsedDatabases()
public List<String> getDatabaseNames()
MongoException
public void dropDatabase(String dbName)
dbName
- name of database to dropMongoException
@Deprecated public String getVersion()
@Deprecated public String debugString()
public String getConnectPoint()
String
representation of current connection point, i.e. master.@Deprecated public DBTCPConnector getConnector()
DBTCPConnector
is NOT part of the public API. It will be dropped in 3.x releases.public ReplicaSetStatus getReplicaSetStatus()
public ServerAddress getAddress()
public List<ServerAddress> getAllAddress()
MongoException
public List<ServerAddress> getServerAddressList()
MongoException
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()
ReadPreference.secondaryPreferred()
ReadPreference.secondaryPreferred()
public void addOption(int option)
option
- value to be added to current optionspublic void setOptions(int options)
options
- value to be setpublic void resetOptions()
public int getOptions()
@Deprecated public MongoOptions getMongoOptions()
MongoClient
and corresponding MongoClient.getMongoClientOptions()
MongoOptions
containing the settings for this MongoDB instance.public int getMaxBsonObjectSize()
MongoException
public CommandResult fsync(boolean async)
async
- if true, the fsync will be done asynchronously on the server.MongoException
public CommandResult fsyncAndLock()
MongoException
public DBObject unlock()
DBObject
in the following form {"ok": 1,"info": "unlock completed"}
MongoException
public boolean isLocked()
MongoException