2929import org .bson .io .PoolOutputBuffer ;
3030
3131/**
32- * A database connection with internal pooling.
33- * For most application, you should have 1 Mongo instance for the entire JVM.
34- *
35- * The following are equivalent, and all connect to the
36- * local database running on the default port:
37- *
38- * <blockquote><pre>
39- * Mongo mongo1 = new Mongo( "127.0.0.1" );
40- * Mongo mongo2 = new Mongo( "127.0.0.1", 27017 );
41- * Mongo mongo3 = new Mongo( new DBAddress( "127.0.0.1", 27017, "test" ) );
42- * Mongo mongo4 = new Mongo( new ServerAddress( "127.0.0.1") );
43- * </pre></blockquote>
44- *
45- * Mongo instances have connection pooling built in - see the requestStart
46- * and requestDone methods for more information.
47- * http://www.mongodb.org/display/DOCS/Java+Driver+Concurrency
48- *
49- * <h3>Connecting to a Replica Set</h3>
32+ * A database connection with internal connection pooling. For most applications, you should have one Mongo instance
33+ * for the entire JVM.
34+ * <p>
35+ * The following are equivalent, and all connect to the local database running on the default port:
36+ * <pre>
37+ * Mongo mongo1 = new Mongo();
38+ * Mongo mongo1 = new Mongo("localhost");
39+ * Mongo mongo2 = new Mongo("localhost", 27017);
40+ * Mongo mongo4 = new Mongo(new ServerAddress("localhost"));
41+ * </pre>
5042 * <p>
5143 * You can connect to a
52- * <a href="http://www.mongodb.org/display/DOCS/Replica+Sets">replica set</a>
53- * using the Java driver by passing several a list if ServerAddress to the
54- * Mongo constructor.
55- * For example:
56- * </p>
57- * <blockquote><pre>
58- * List<ServerAddress> addrs = new ArrayList<ServerAddress>();
59- * addrs.add( new ServerAddress( "127.0.0.1" , 27017 ) );
60- * addrs.add( new ServerAddress( "127.0.0.1" , 27018 ) );
61- * addrs.add( new ServerAddress( "127.0.0.1" , 27019 ) );
62- *
63- * Mongo mongo = new Mongo( addrs );
64- * </pre></blockquote>
65- *
44+ * <a href="http://www.mongodb.org/display/DOCS/Replica+Sets">replica set</a> using the Java driver by passing
45+ * a ServerAddress list to the Mongo constructor. For example:
46+ * <pre>
47+ * Mongo mongo = new Mongo(Arrays.asList(
48+ * new ServerAddress("localhost", 27017),
49+ * new ServerAddress("localhost", 27018),
50+ * new ServerAddress("localhost", 27019)));
51+ * </pre>
52+ * You can connect to a sharded cluster using the same constructor. Mongo will auto-detect whether the servers are
53+ * a list of replica set members or a list of mongos servers.
54+ * <p>
55+ * By default, all read and write operations will be made on the primary,
56+ * but it's possible to read from secondaries by changing the read preference:
6657 * <p>
67- * By default, all read and write operations will be made on the master.
68- * But it's possible to read from the slave(s) by using slaveOk:
69- * </p>
70- * <blockquote><pre>
71- * mongo.slaveOk();
72- * </pre></blockquote>
58+ * <pre>
59+ * mongo.setReadPreference(ReadPreference.secondary());
60+ * </pre>
61+ * By default, write operations will not throw exceptions on failure, but that is easily changed too:
62+ * <p>
63+ * <pre>
64+ * mongo.setWriteConcern(WriteConcern.SAFE);
65+ * </pre>
66+ *
67+ * @see com.mongodb.ReadPreference
68+ * @see com.mongodb.WriteConcern
7369 */
7470public class Mongo {
7571
@@ -243,10 +239,14 @@ public Mongo( ServerAddress left , ServerAddress right , MongoOptions options )
243239 }
244240
245241 /**
246- * <p> Creates a Mongo based on a replica set, or pair .
242+ * Creates a Mongo based on a list of replica set members or a list of mongos .
247243 * It will find all members (the master will be used by default). If you pass in a single server in the list,
248244 * the driver will still function as if it is a replica set. If you have a standalone server,
249- * use the Mongo(ServerAddress) constructor.</p>
245+ * use the Mongo(ServerAddress) constructor.
246+ * <p>
247+ * If this is a list of mongos servers, it will pick the closest (lowest ping time) one to send all requests to,
248+ * and automatically fail over to the next server if the closest is down.
249+ *
250250 * @see com.mongodb.ServerAddress
251251 * @param seeds Put as many servers as you can in the list and the system will figure out the rest. This can
252252 * either be a list of mongod servers in the same replica set or a list of mongos servers in the same
@@ -258,13 +258,19 @@ public Mongo( List<ServerAddress> seeds ) {
258258 }
259259
260260 /**
261- * <p>Creates a Mongo based on a replica set, or pair.
262- * It will find all members (the master will be used by default).</p>
261+ * Creates a Mongo based on a list of replica set members or a list of mongos.
262+ * It will find all members (the master will be used by default). If you pass in a single server in the list,
263+ * the driver will still function as if it is a replica set. If you have a standalone server,
264+ * use the Mongo(ServerAddress) constructor.
265+ * <p>
266+ * If this is a list of mongos servers, it will pick the closest (lowest ping time) one to send all requests to,
267+ * and automatically fail over to the next server if the closest is down.
268+ *
263269 * @see com.mongodb.ServerAddress
264270 * @param seeds Put as many servers as you can in the list and the system will figure out the rest. This can
265271 * either be a list of mongod servers in the same replica set or a list of mongos servers in the same
266272 * sharded cluster.
267- * @param options default query options
273+ * @param options for configuring this Mongo instance
268274 * @throws MongoException
269275 */
270276 public Mongo ( List <ServerAddress > seeds , MongoOptions options ) {
@@ -289,8 +295,8 @@ public Mongo( List<ServerAddress> seeds , MongoOptions options ) {
289295 * @param uri
290296 * @see MongoURI
291297 * <p>examples:
292- * <li>mongodb://127.0.0.1 </li>
293- * <li>mongodb://fred:foobar@127.0.0.1 /</li>
298+ * <li>mongodb://localhost </li>
299+ * <li>mongodb://fred:foobar@localhost /</li>
294300 * </p>
295301 * @throws MongoException
296302 * @throws UnknownHostException
0 commit comments