java.lang.Object | |
↳ | com.mongodb.Mongo |
A database connection with internal pooling. For most application, you should have 1 Mongo instance for the entire JVM. The following are equivalent, and all connect to the local database running on the default port:
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();
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Mongo.Holder | Mongo.Holder can be used as a static place to hold several instances of Mongo. |
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | MAJOR_VERSION |
This constant is deprecated.
Replaced by Mongo.getMajorVersion()
|
|||||||||
int | MINOR_VERSION |
This constant is deprecated.
Replaced by Mongo.getMinorVersion()
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Creates a Mongo instance based on a (single) mongodb node (localhost, default port)
| |||||||||||
Creates a Mongo instance based on a (single) mongodb node (default port)
| |||||||||||
Creates a Mongo instance based on a (single) mongodb node (default port)
| |||||||||||
Creates a Mongo instance based on a (single) mongodb node
| |||||||||||
Creates a Mongo instance based on a (single) mongodb node
| |||||||||||
Creates a Mongo instance based on a (single) mongo node using a given ServerAddress
| |||||||||||
Creates a Mongo in paired mode. | |||||||||||
Creates a Mongo connection in paired mode. | |||||||||||
Creates a Mongo based on a replica set, or pair. | |||||||||||
Creates a Mongo based on a replica set, or pair. | |||||||||||
Creates a Mongo described by a URI.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
adds a default query option
| |||||||||||
closes the underlying connector, which in turn closes all open connections.
| |||||||||||
returns a database object
| |||||||||||
returns a string representing the hosts used in this Mongo instance
| |||||||||||
Drops the database if it exists.
| |||||||||||
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.
| |||||||||||
Forces the master server to fsync the RAM data to disk, then lock all writes.
| |||||||||||
Gets the address of the current master
| |||||||||||
Gets a list of all server addresses used when this Mongo was created
| |||||||||||
Gets the current master's hostname
| |||||||||||
Gets the underlying TCP connector
| |||||||||||
gets a database object
| |||||||||||
gets a list of all database names present on the server@return
| |||||||||||
Gets the major version of this library
| |||||||||||
Gets the maximum size for a BSON object supported by the current master server.
| |||||||||||
Gets the minor version of this library
| |||||||||||
Returns the mongo options.
| |||||||||||
gets the default query options
| |||||||||||
Gets the default read preference
| |||||||||||
Gets the replica set status object
| |||||||||||
Gets the list of server addresses currently seen by the connector.
| |||||||||||
gets a collection of DBs used by the driver since this Mongo instance was created.
| |||||||||||
gets this driver version
| |||||||||||
Gets the default write concern
| |||||||||||
Returns true if the database is locked (read-only), false otherwise.
| |||||||||||
reset the default query options
| |||||||||||
sets the default query options
| |||||||||||
Sets the read preference for this database.
| |||||||||||
Sets the write concern for this database.
| |||||||||||
This method is deprecated.
Replaced with ReadPreference.SECONDARY
| |||||||||||
Unlocks the database, allowing the write operations to go through.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
|
This constant is deprecated.
Replaced by Mongo.getMajorVersion()
This constant is deprecated.
Replaced by Mongo.getMinorVersion()
Creates a Mongo instance based on a (single) mongodb node (localhost, default port)
UnknownHostException | |
MongoException | |
UnknownHostException |
Creates a Mongo instance based on a (single) mongodb node (default port)
host | server to connect to |
---|
UnknownHostException | if the database host cannot be resolved |
---|---|
MongoException |
Creates a Mongo instance based on a (single) mongodb node (default port)
host | server to connect to |
---|---|
options | default query options |
UnknownHostException | if the database host cannot be resolved |
---|---|
MongoException |
Creates a Mongo instance based on a (single) mongodb node
host | the database's host address |
---|---|
port | the port on which the database is running |
UnknownHostException | if the database host cannot be resolved |
---|---|
MongoException |
Creates a Mongo instance based on a (single) mongodb node
addr | the database address |
---|
MongoException |
---|
Creates a Mongo instance based on a (single) mongo node using a given ServerAddress
addr | the database address |
---|---|
options | default query options |
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 pair |
---|---|
right | right side of the pair |
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 pair |
---|---|
right | right side of the pair |
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 |
---|
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 out |
---|---|
options | default query options |
MongoException |
---|
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.
MongoException | |
UnknownHostException | |
MongoException | |
UnknownHostException |
adds a default query option
closes the underlying connector, which in turn closes all open connections. Once called, this Mongo instance can no longer be used.
returns a database object
addr | the database address |
---|
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.
async | if true, the fsync will be done asynchronously on the server. |
---|
Forces the master server to fsync the RAM data to disk, then lock all writes. The database will be read-only after this command returns.
Gets a list of all server addresses used when this Mongo was created
gets a list of all database names present on the server@return
MongoException |
---|
Gets the major version of this library
Gets the maximum size for a BSON object supported by the current master server. Note that this value may change over time depending on which server is master. If the size is not known yet, a request may be sent to the master server
Gets the minor version of this library
gets the default query options
Gets the list of server addresses currently seen by the connector. This includes addresses auto-discovered from a replica set.
gets a collection of DBs used by the driver since this Mongo instance was created. This may include DBs that exist in the client but not yet on the server.
Returns true if the database is locked (read-only), false otherwise.
reset the default query options
sets the default query options
Sets the read preference for this database. Will be used as default for
reads from any collection in any database. See the
documentation for ReadPreference
for more information.
preference | Read Preference to use |
---|
Sets the write concern for this database. Will be used as default for
writes to any collection in any database. See the
documentation for WriteConcern
for more information.
concern | write concern to use |
---|
This method is deprecated.
Replaced with ReadPreference.SECONDARY
makes it possible to run read queries on slave nodes
Unlocks the database, allowing the write operations to go through. This command may be asynchronous on the server, which means there may be a small delay before the database becomes writable.