@ThreadSafe public class Mongo extends java.lang.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.
|
private static class |
Mongo.ServerCursorAndNamespace |
Modifier and Type | Field and Description |
---|---|
(package private) static java.lang.String |
ADMIN_DATABASE_NAME |
private BufferProvider |
bufferProvider |
private Cluster |
cluster |
private java.util.List<MongoCredential> |
credentialsList |
private java.util.concurrent.ExecutorService |
cursorCleaningService |
private java.util.concurrent.ConcurrentMap<java.lang.String,DB> |
dbCache |
private Bytes.OptionHolder |
optionHolder |
private MongoClientOptions |
options |
private java.util.concurrent.ConcurrentLinkedQueue<Mongo.ServerCursorAndNamespace> |
orphanedCursors |
private ReadConcern |
readConcern |
private ReadPreference |
readPreference |
private ServerSessionPool |
serverSessionPool |
private WriteConcern |
writeConcern |
Constructor and Description |
---|
Mongo()
Deprecated.
Replaced by
MongoClient() ) |
Mongo(Cluster cluster,
MongoClientOptions options,
java.util.List<MongoCredential> credentialsList) |
Mongo(java.util.List<ServerAddress> seeds)
Deprecated.
Replaced by
MongoClient(java.util.List) |
Mongo(java.util.List<ServerAddress> seedList,
java.util.List<MongoCredential> credentialsList,
MongoClientOptions options) |
Mongo(java.util.List<ServerAddress> seedList,
java.util.List<MongoCredential> credentialsList,
MongoClientOptions options,
MongoDriverInformation mongoDriverInformation) |
Mongo(java.util.List<ServerAddress> seedList,
MongoClientOptions options) |
Mongo(java.util.List<ServerAddress> seeds,
MongoOptions options)
Deprecated.
Replaced by
MongoClient(java.util.List, MongoClientOptions) |
Mongo(MongoClientURI mongoURI) |
Mongo(MongoClientURI mongoURI,
MongoDriverInformation mongoDriverInformation) |
Mongo(MongoURI uri)
Deprecated.
Replaced by
MongoClient(MongoClientURI) |
Mongo(ServerAddress address)
Deprecated.
Replaced by
MongoClient(ServerAddress) |
Mongo(ServerAddress serverAddress,
java.util.List<MongoCredential> credentialsList,
MongoClientOptions options) |
Mongo(ServerAddress serverAddress,
java.util.List<MongoCredential> credentialsList,
MongoClientOptions options,
MongoDriverInformation mongoDriverInformation) |
Mongo(ServerAddress serverAddress,
MongoClientOptions options) |
Mongo(ServerAddress address,
MongoOptions options)
Deprecated.
Replaced by
MongoClient(ServerAddress, MongoClientOptions) |
Mongo(ServerAddress left,
ServerAddress right)
Deprecated.
Please use
MongoClient(java.util.List) instead. |
Mongo(ServerAddress left,
ServerAddress right,
MongoOptions options)
Deprecated.
Please use
MongoClient(java.util.List, MongoClientOptions) instead. |
Mongo(java.lang.String host)
Deprecated.
Replaced by
MongoClient(String) |
Mongo(java.lang.String host,
int port)
Deprecated.
Replaced by
MongoClient(String, int) |
Mongo(java.lang.String host,
MongoOptions options)
Deprecated.
Replaced by
MongoClient(String, MongoClientOptions) |
Modifier and Type | Method and Description |
---|---|
void |
addOption(int option)
Deprecated.
Add options instead on instances of
DBCursor |
(package private) void |
addOrphanedCursor(ServerCursor serverCursor,
MongoNamespace namespace) |
private void |
cleanCursors() |
void |
close()
Closes all resources associated with this instance, in particular any open network connections.
|
(package private) ClientSession |
createClientSession(ClientSessionOptions options) |
private static Cluster |
createCluster(ClusterSettings clusterSettings,
java.util.List<MongoCredential> credentialsList,
MongoClientOptions options,
MongoDriverInformation mongoDriverInformation) |
private static Cluster |
createCluster(java.util.List<ServerAddress> seedList,
java.util.List<MongoCredential> credentialsList,
MongoClientOptions options,
MongoDriverInformation mongoDriverInformation) |
private static Cluster |
createCluster(MongoClientURI mongoURI,
MongoDriverInformation mongoDriverInformation) |
private static Cluster |
createCluster(ServerAddress serverAddress,
java.util.List<MongoCredential> credentialsList,
MongoClientOptions options,
MongoDriverInformation mongoDriverInformation) |
private java.util.concurrent.ExecutorService |
createCursorCleaningService() |
private static MongoClientOptions |
createLegacyOptions() |
(package private) OperationExecutor |
createOperationExecutor() |
void |
dropDatabase(java.lang.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
|
java.util.List<ServerAddress> |
getAllAddress()
Gets a list of all server addresses used when this Mongo was created
|
(package private) BufferProvider |
getBufferProvider() |
(package private) Cluster |
getCluster() |
private ClusterDescription |
getClusterDescription() |
private static ClusterSettings |
getClusterSettings(java.util.List<ServerAddress> seedList,
MongoClientOptions options,
ClusterConnectionMode clusterConnectionMode) |
private ClusterDescription |
getConnectedClusterDescription() |
java.lang.String |
getConnectPoint()
Gets a
String representation of current connection point, i.e. |
(package private) java.util.List<MongoCredential> |
getCredentialsList() |
java.util.List<java.lang.String> |
getDatabaseNames()
Deprecated.
Replaced with
MongoClient.listDatabaseNames() |
DB |
getDB(java.lang.String dbName)
Deprecated.
|
int |
getMaxBsonObjectSize()
Gets the maximum size for a BSON object supported by the current master server.
|
(package private) MongoClientOptions |
getMongoClientOptions() |
MongoOptions |
getMongoOptions()
Deprecated.
Please use
MongoClient class to connect to server and corresponding MongoClient.getMongoClientOptions() |
(package private) Bytes.OptionHolder |
getOptionHolder() |
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.
|
java.util.List<ServerAddress> |
getServerAddressList()
Gets the list of server addresses currently seen by this client.
|
private java.util.List<ServerDescription> |
getServerDescriptionListToConsiderForSessionSupport(ClusterDescription clusterDescription) |
private static ServerSelector |
getServerSelector(MongoClientOptions options) |
(package private) ServerSessionPool |
getServerSessionPool() |
private static ClusterConnectionMode |
getSingleServerClusterMode(MongoClientOptions options) |
java.util.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() |
java.lang.String |
toString() |
DBObject |
unlock()
Unlocks the database, allowing the write operations to go through.
|
static final java.lang.String ADMIN_DATABASE_NAME
private final java.util.concurrent.ConcurrentMap<java.lang.String,DB> dbCache
private volatile WriteConcern writeConcern
private volatile ReadPreference readPreference
private final ReadConcern readConcern
private final MongoClientOptions options
private final java.util.List<MongoCredential> credentialsList
private final Bytes.OptionHolder optionHolder
private final Cluster cluster
private final BufferProvider bufferProvider
private final java.util.concurrent.ConcurrentLinkedQueue<Mongo.ServerCursorAndNamespace> orphanedCursors
private final java.util.concurrent.ExecutorService cursorCleaningService
private final ServerSessionPool serverSessionPool
@Deprecated public Mongo()
MongoClient()
)MongoException
- if there's a failure@Deprecated public Mongo(java.lang.String host)
MongoClient(String)
host
- server to connect to@Deprecated public Mongo(java.lang.String host, MongoOptions options)
MongoClient(String, MongoClientOptions)
host
- server to connect tooptions
- default query options@Deprecated public Mongo(java.lang.String host, int port)
MongoClient(String, int)
host
- the host address of the databaseport
- the port on which the database is running@Deprecated public Mongo(ServerAddress address)
MongoClient(ServerAddress)
address
- the database addressServerAddress
@Deprecated public Mongo(ServerAddress address, MongoOptions options)
MongoClient(ServerAddress, MongoClientOptions)
address
- the database addressoptions
- default query optionsServerAddress
@Deprecated public Mongo(ServerAddress left, ServerAddress right)
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(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(java.util.List<ServerAddress> seeds)
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(java.util.List<ServerAddress> seeds, MongoOptions options)
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(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
Mongo(java.util.List<ServerAddress> seedList, MongoClientOptions options)
Mongo(ServerAddress serverAddress, MongoClientOptions options)
Mongo(ServerAddress serverAddress, java.util.List<MongoCredential> credentialsList, MongoClientOptions options)
Mongo(ServerAddress serverAddress, java.util.List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation)
Mongo(java.util.List<ServerAddress> seedList, java.util.List<MongoCredential> credentialsList, MongoClientOptions options)
Mongo(java.util.List<ServerAddress> seedList, java.util.List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation)
Mongo(MongoClientURI mongoURI)
Mongo(MongoClientURI mongoURI, MongoDriverInformation mongoDriverInformation)
Mongo(Cluster cluster, MongoClientOptions options, java.util.List<MongoCredential> credentialsList)
@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 java.util.List<ServerAddress> getAllAddress()
MongoException
- if there's a failurepublic java.util.List<ServerAddress> getServerAddressList()
MongoException
- if there's a failureprivate ClusterDescription getClusterDescription()
public 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 java.util.List<java.lang.String> getDatabaseNames()
MongoClient.listDatabaseNames()
MongoException
- if the operation fails@Deprecated public DB getDB(java.lang.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 retrievejava.lang.IllegalArgumentException
- if the name is invalidMongoNamespace.checkDatabaseNameValidity(String)
public java.util.Collection<DB> getUsedDatabases()
public void dropDatabase(java.lang.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 java.lang.String toString()
toString
in class java.lang.Object
public int getMaxBsonObjectSize()
MongoException
- if there's a failurepublic java.lang.String getConnectPoint()
String
representation of current connection point, i.e. master.private static MongoClientOptions createLegacyOptions()
private static Cluster createCluster(MongoClientURI mongoURI, MongoDriverInformation mongoDriverInformation)
private static Cluster createCluster(java.util.List<ServerAddress> seedList, java.util.List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation)
private static Cluster createCluster(ServerAddress serverAddress, java.util.List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation)
private static Cluster createCluster(ClusterSettings clusterSettings, java.util.List<MongoCredential> credentialsList, MongoClientOptions options, MongoDriverInformation mongoDriverInformation)
private static ClusterSettings getClusterSettings(java.util.List<ServerAddress> seedList, MongoClientOptions options, ClusterConnectionMode clusterConnectionMode)
private static ServerSelector getServerSelector(MongoClientOptions options)
Cluster getCluster()
ServerSessionPool getServerSessionPool()
Bytes.OptionHolder getOptionHolder()
BufferProvider getBufferProvider()
MongoClientOptions getMongoClientOptions()
java.util.List<MongoCredential> getCredentialsList()
void addOrphanedCursor(ServerCursor serverCursor, MongoNamespace namespace)
OperationExecutor createOperationExecutor()
ClientSession createClientSession(ClientSessionOptions options)
private ClusterDescription getConnectedClusterDescription()
private java.util.List<ServerDescription> getServerDescriptionListToConsiderForSessionSupport(ClusterDescription clusterDescription)
private java.util.concurrent.ExecutorService createCursorCleaningService()
private void cleanCursors()
private static ClusterConnectionMode getSingleServerClusterMode(MongoClientOptions options)