Mongo mongo = new Mongo(Arrays.asList( new ServerAddress("localhost", 27017), new ServerAddress("localhost", 27018), new ServerAddress("localhost", 27019)));You can connect to a sharded cluster using the same constructor. Mongo will auto-detect whether the servers are a list of replica set members or a list of mongos servers.
By default, all read and write operations will be made on the primary, but it's possible to read from secondaries by changing the read preference:
mongo.setReadPreference(ReadPreference.secondary());By default, write operations will not throw exceptions on failure, but that is easily changed too:
mongo.setWriteConcern(WriteConcern.SAFE);Note: This class has been superseded by {@code MongoClient}, and may be deprecated in a future release. @see MongoClient @see ReadPreference @see WriteConcern
|
|