@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)
Deprecated.
Add options instead on instances of
DBCursor |
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()
Deprecated.
Replaced with
MongoClient.listDatabaseNames() |
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()
Deprecated.
Get options instead from instances of
DBCursor |
ReadConcern |
getReadConcern()
Gets the read concern
|
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 write concern
|
boolean |
isLocked()
Returns true if the database is locked (read-only), false otherwise.
|
void |
resetOptions()
Deprecated.
Reset options instead on instances of
DBCursor |
void |
setOptions(int options)
Deprecated.
Set options on instances of
DBCursor |
void |
setReadPreference(ReadPreference readPreference)
Deprecated.
Set the default read preference with either
MongoClientURI or MongoClientOptions |
void |
setWriteConcern(WriteConcern writeConcern)
Deprecated.
Set the default write concern with either
MongoClientURI or MongoClientOptions |
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 an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members.
For a list with a single seed, the driver will still discover all members of the replica set. For a direct
connection to a replica set member, with no discovery, use the Mongo(ServerAddress)
constructor instead.
When there is more than one server to choose from based on the type of request (read or write) and the read preference (if it's a
read request), the driver will randomly select a server to send a request. This applies to both replica sets and sharded clusters.
The servers to randomly select from are further limited by the local threshold. See
MongoClientOptions.getLocalThreshold()
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.MongoClientOptions.getLocalThreshold()
@Deprecated public Mongo(List<ServerAddress> seeds, MongoOptions options)
MongoClient.MongoClient(java.util.List, MongoClientOptions)
Creates an instance based on a list of replica set members or mongos servers. For a replica set it will discover all members.
For a list with a single seed, the driver will still discover all members of the replica set. For a direct
connection to a replica set member, with no discovery, use the Mongo(ServerAddress, MongoClientOptions)
constructor
instead.
When there is more than one server to choose from based on the type of request (read or write) and the read preference (if it's a
read request), the driver will randomly select a server to send a request. This applies to both replica sets and sharded clusters.
The servers to randomly select from are further limited by the local threshold. See
MongoClientOptions.getLocalThreshold()
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
- the optionsMongoClientOptions.getLocalThreshold()
@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
@Deprecated public void setWriteConcern(WriteConcern writeConcern)
MongoClientURI
or MongoClientOptions
DBCollection
created indirectly from this
instance, via a DB
instance created from getDB(String)
.
Note that changes to the default write concern made via this method will NOT affect the write concern of
MongoDatabase
instances created via MongoClient.getDatabase(String)
writeConcern
- write concern to usepublic WriteConcern getWriteConcern()
public ReadConcern getReadConcern()
@Deprecated public void setReadPreference(ReadPreference readPreference)
MongoClientURI
or MongoClientOptions
DBCollection
created indirectly from this
instance, via a DB
instance created from getDB(String)
.
Note that changes to the default read preference made via this method will NOT affect the read preference of
MongoDatabase
instances created via MongoClient.getDatabase(String)
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()
@Deprecated public List<String> getDatabaseNames()
MongoClient.listDatabaseNames()
MongoException
- if the operation fails@Deprecated public DB getDB(String dbName)
MongoClient.getDatabase(String)
MongoClient.getDatabase(String)
instead.
The DB
class has been superseded by MongoDatabase
. The deprecation of this method effectively
deprecates the DB
, DBCollection
, and DBCursor
classes, among others; but in order to give users time to
migrate to the new API without experiencing a huge number of compiler warnings, those classes have not yet been formally
deprecated.
dbName
- the name of the database to retrieveIllegalArgumentException
- if the name is invalidMongoNamespace.checkDatabaseNameValidity(String)
public 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()
@Deprecated public void setOptions(int options)
DBCursor
DBCollection
created indirectly from this
instance, via a DB
instance created from getDB(String)
.
Note that changes to query options made via this method will NOT affect
MongoDatabase
instances created via MongoClient.getDatabase(String)
options
- value to be set@Deprecated public void resetOptions()
DBCursor
DBCollection
created indirectly from this
instance, via a DB
instance created from getDB(String)
.@Deprecated public void addOption(int option)
DBCursor
DBCollection
created indirectly from this
instance, via a DB
instance created from getDB(String)
.
Note that changes to query options made via this method will NOT affect
MongoDatabase
instances created via MongoClient.getDatabase(String)
option
- value to be added to current options@Deprecated public int getOptions()
DBCursor
DBCollection
created indirectly from this
instance, via a DB
instance created from getDB(String)
.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.