MongoClient mongoClient = new MongoClient(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. MongoClient 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:
mongoClient.setReadPreference(ReadPreference.secondaryPreferred());By default, all write operations will wait for acknowledgment by the server, as the default write concern is {@code WriteConcern.ACKNOWLEDGED}.
Note: This class supersedes the {@code Mongo} class. While it extends {@code Mongo}, it differs from it in that the default write concern is to wait for acknowledgment from the server of all write operations. In addition, its constructors accept instances of {@code MongoClientOptions} and {@code MongoClientURI}, which both also set the same default write concern.
In general, users of this class will pick up all of the default options specified in {@code MongoClientOptions}. In particular, note that the default value of the connectionsPerHost option has been increased to 100 from the old default value of 10 used by the superseded {@code Mongo} class. @see ReadPreference#primary() @see com.massivecraft.mcore.xlib.mongodb.WriteConcern#ACKNOWLEDGED @see MongoClientOptions @see MongoClientURI @since 2.10.0
|
|
|
|