MongoClient类参考文档
(MongoClient類的實例是)一個帶有內部連接池的MongoDB客戶端。對絕大部分應用來說,都應該有一個MongoClient實例(不知怎翻譯)。
下面各種創建實例的方法是等效的,并且全部都通過默認端口連到本地數據庫的:
MongoClient mongoClient1 = new MongoClient();
MongoClient mongoClient1 = new MongoClient(“localhost”);
MongoClient mongoClient2 = new MongoClient(“localhost”, 27017);
MongoClient mongoClient4 = new MongoClient(new ServerAddress(“localhost”));
MongoClient mongoClient5 = new MongoClient(new ServerAddress(“localhost”), new MongoClientOptions.Builder().build());
你也可以連接到備份服務器集合,通過傳遞一個備份服務器地址列表作為參數到MongoClient的構造方法中。例如:
MongoClient mongoClient = new MongoClient(Arrays.asList(
new ServerAddress(“localhost”, 27017),
new ServerAddress(“localhost”, 27018),
new ServerAddress(“localhost”, 27019)));
你還可以使用同樣的構造方法連接到分片集群上。
MongoClient會自動檢測這些數據庫服務器是備份服務器還是集群服務器。
默認情況下,所有讀寫操作都發生在主數據庫上,但也可以改變讀的設置讓讀操作發生在從數據庫: mongoClient.setReadPreference(ReadPreference.secondaryPreferred());
默認情況下,所有寫操作都會等待服務器的響應,默認的寫關注的是:WriteConcern.ACKNOWLEDGED
提示:MongoClient類取代了Mongo類。同時它也繼承自Mongo類,但不同的是,它所有的寫操作默認都是等待服務器響應。另外,它的構造方法接受MongoClientOptions和 MongoClientURI類的實例,這兩者的默認寫關注也是一樣的。
通常來說,使用這個類只要使用MongoClientOptions提供的默認值就行了。特別注意一下,每個服務器默認的連接數已經從過去使用Mongo的10增加到了100.
A MongoDB client with internal connection pooling. For most applications, you should have one MongoClient instance for the entire JVM.
The following are equivalent, and all connect to the local database running on the default port:
MongoClient mongoClient1 = new MongoClient();
MongoClient mongoClient1 = new MongoClient(“localhost”);
MongoClient mongoClient2 = new MongoClient(“localhost”, 27017);
MongoClient mongoClient4 = new MongoClient(new ServerAddress(“localhost”));
MongoClient mongoClient5 = new MongoClient(new ServerAddress(“localhost”), new MongoClientOptions.Builder().build());
You can connect to a replica set using the Java driver by passing a ServerAddress list to the MongoClient constructor. For example:
MongoClient mongoClient = new MongoClient(Arrays.asList(
new ServerAddress(“localhost”, 27017),
new ServerAddress(“localhost”, 27018),
new ServerAddress(“localhost”, 27019)));
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 WriteConcern.ACKNOWLEDGED.
Note: This class supersedes the Mongo class. While it extends 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 MongoClientOptions and 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 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 Mongo class.
Since:2.10.0See Also:ReadPreference.primary()com.mongodb.WriteConcern.ACKNOWLEDGEDMongoClientOptionsMongoClientURI
總結
以上是生活随笔為你收集整理的MongoClient类参考文档的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python能开发什么产品_三周学 Py
- 下一篇: 神舟战神换cpu教程_神舟将十代i5称为