日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

MongoClient类参考文档

發布時間:2023/12/10 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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类参考文档的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。