@ThreadSafe 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.
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 | Method and Description |
---|---|
void |
addOption(int option)
Add the default query option.
|
void |
close()
Closes all resources associated with this instance, in particular any open network connections.
|
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
|
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)
Deprecated.
|
int |
getMaxBsonObjectSize()
Gets the maximum size for a BSON object supported by the current master server.
|
MongoOptions |
getMongoOptions()
Deprecated.
Please use
MongoClient class to connect to server 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.
|
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 readPreference)
Sets the read preference for this database.
|
void |
setWriteConcern(WriteConcern writeConcern)
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 Mongo()
MongoClient.MongoClient()
)MongoException
- if there's a failure@Deprecated public Mongo(String host)
MongoClient.MongoClient(String)
host
- server to connect to@Deprecated public Mongo(String host, MongoOptions options)
MongoClient.MongoClient(String, MongoClientOptions)
host
- server to connect tooptions
- default query options@Deprecated public Mongo(String host, int port)
MongoClient.MongoClient(String, int)
host
- the host address of the databaseport
- the port on which the database is running@Deprecated public Mongo(ServerAddress address)
MongoClient.MongoClient(ServerAddress)
address
- the database addressServerAddress
@Deprecated public Mongo(ServerAddress address, MongoOptions options)
MongoClient.MongoClient(ServerAddress, MongoClientOptions)
address
- the database addressoptions
- default query optionsServerAddress
@Deprecated public Mongo(ServerAddress left, ServerAddress right)
MongoClient.MongoClient(java.util.List)
instead.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 pairServerAddress
@Deprecated public Mongo(ServerAddress left, ServerAddress right, MongoOptions options)
MongoClient.MongoClient(java.util.List, MongoClientOptions)
instead.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
- 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)
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 credentialsMongoException
- if there's a failureMongoURI
public void setWriteConcern(WriteConcern writeConcern)
WriteConcern
for more information.writeConcern
- write concern to usepublic WriteConcern getWriteConcern()
public void setReadPreference(ReadPreference readPreference)
ReadPreference
for more information.readPreference
- Read Preference to usepublic ReadPreference getReadPreference()
public List<ServerAddress> getAllAddress()
MongoException
- if there's a failurepublic List<ServerAddress> getServerAddressList()
MongoException
- if there's a failurepublic ServerAddress getAddress()
@Deprecated public MongoOptions getMongoOptions()
MongoClient
class to connect to server and corresponding MongoClient.getMongoClientOptions()
Returns the mongo options.
Changes to MongoOptions
that are done after connection are not reflected.
public ReplicaSetStatus getReplicaSetStatus()
public List<String> getDatabaseNames()
MongoException
- if the operation fails@Deprecated public DB getDB(String dbName)
MongoClient.getDatabase(String)
MongoClient.getDatabase(String)
instead.dbName
- the name of the database to retrievepublic Collection<DB> getUsedDatabases()
public void dropDatabase(String dbName)
dbName
- name of database to dropMongoException
- if the operation failspublic void close()
@Deprecated public void slaveOk()
ReadPreference.secondaryPreferred()
ReadPreference.secondaryPreferred()
public void setOptions(int options)
options
- value to be setpublic void resetOptions()
public void addOption(int option)
option
- value to be added to current optionspublic int getOptions()
public CommandResult fsync(boolean async)
async
- if true, the fsync will be done asynchronously on the server.MongoException
- if there's a failurepublic CommandResult fsyncAndLock()
MongoException
- if there's a failurepublic DBObject unlock()
DBObject
in the following form {"ok": 1,"info": "unlock completed"}
MongoException
- if there's a failurepublic boolean isLocked()
MongoException
- if the operation failspublic int getMaxBsonObjectSize()
MongoException
- if there's a failurepublic String getConnectPoint()
String
representation of current connection point, i.e. master.