Overview
このセクションでは、 Kotlin Sync ドライバーで使用できる MongoDB の接続オプションと認証オプションについて説明します。 接続を構成するには、接続 URI または MongoClientSettingsインスタンス内でオプションを設定します。
接続 URI でオプションを設定する
接続 URI をMongoClient.create()メソッドに渡す場合は、接続オプションを<name>=<value>ペアとして string に含めることができます。 次の例では、接続 URIconnectTimeoutMS に、値が60000 のtls オプションと、値が のtrue オプションが含まれています。
val uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true" val mongoClient = MongoClient.create(uri) 
MongoClientSettings でオプションを設定する
MongoClientSettings.Builderクラスのメソッドを使用して、 MongoClientSettingsインスタンスで接続オプションを設定するには、設定オブジェクトをMongoClient.create()メソッドに渡します。
このように接続を構成すると、実行時に設定を変更しやすくなり、コンパイル時にエラーを検出しやすくなります。
次の例は、 MongoClientSettingsインスタンスの作成時に接続ターゲットを指定する方法と、その他のオプションを設定する方法を示しています。
val settings = MongoClientSettings.builder()     .applyToClusterSettings { builder -> builder.hosts(listOf(ServerAddress("localhost", 27017))) }     .applyToSocketSettings { builder -> builder.connectTimeout(60000, TimeUnit.MILLISECONDS) }     .applyToSslSettings { builder -> builder.enabled(true) }     .build()  val mongoClient = MongoClient.create(settings) 
ホスト名とポートを指定する代わりに接続stringを指定する場合は、applyConnectionString() メソッドを使用し、次のコードに示すようにビルダー メソッドを使用して他のオプションを設定します。
val uri = "<connection string>" val settings = MongoClientSettings.builder()     .applyConnectionString(ConnectionString(uri))     .applyToSocketSettings { builder -> builder.connectTimeout(60000, TimeUnit.MILLISECONDS) }     .applyToSslSettings { builder -> builder.enabled(true) }     .build() val mongoClient = MongoClient.create(settings) 
接続オプション
次のセクションでは、 Kotlin Sync ドライバーで使用できる接続オプションについて説明します。 各オプションは、接続 URI で使用できるオプションと値のペアと、使用可能な場合はそれをMongoClientSettingsインスタンス内に設定するためのドライバー メソッドを示します。
ネットワーク圧縮
| 接続オプション | 説明 | 
|---|---|
| compressors | The preferred compression types, in order, for wire-protocol messages sent to
or received from the server. The driver uses the first of these compression types
that the server supports. Data Type: comma-delimited string MongoClientSettings:  compressorList(listOf(<MongoCompressor>))Connection URI:  compressors=snappy,zstd,zlib | 
| zlibCompressionLevel | The compression level for zlib to use. This option accepts
an integer value between  -1and9, corresponding to the
following settings:- -1: (Default). zlib uses its default compression level (usually  6).- 0: No compression. - 1: Fastest speed but lowest compression. - 9: Best compression but slowest speed. Data Type: integer Default:  -1MongoClientSettings:  compressorList(listOf(zlib.withProperty(MongoCompressor.LEVEL, 3)))Connection URI:  zlibCompressionLevel=3 | 
タイムアウト
| 接続オプション | 説明 | |||
|---|---|---|---|---|
| connectTimeoutMS | The time in milliseconds to attempt a connection before timing out. Data Type: integer Default:  10000MongoClientSettings: Connection URI:  timeoutMs=10000 | |||
| socketTimeoutMS (非推奨) | This option is deprecated. You can configure this timeout by setting the the client-level timeout instead. Milliseconds to attempt a send or receive on a connection before the attempt times out. Data Type: integer Default: no timeout MongoClientSettings: Connection URI:  socketTimeoutMS=5000 | 
サーバーの選択
| 接続オプション | 説明 | |||
|---|---|---|---|---|
| serverSelectionTimeoutMS | The maximum amount of time, in milliseconds, the driver waits
for server selection to succeed before throwing an
exception. Data Type: integer Default:  30000MongoClientSettings: Connection URI:  serverSelectionTimeoutMS=30000 | 
認証
Tip
認証オプションの詳細については、認証メカニズムセクションを参照してください。
| 接続オプション | 説明 | |||
|---|---|---|---|---|
| authMechanism | The mechanism that the Kotlin Sync driver uses to authenticate
the application. Data Type: string Default:  "SCRAM-SHA-256"when connecting to MongoDB
v4.0 or laterMongoClientSettings: Connection URI:  authMechanism=SCRAM-SHA-256 | |||
| authMechanismProperties | Options specific to the authentication mechanism. This option
isn't needed for all authentication mechanisms. Data Type: string Connection URI:  authMechanismProperties=AWS_SESSION_TOKEN:12435 | |||
| authSource | The database to authenticate against. Data Type: string Default:  "admin"Connection URI:  authSource=admin | |||
| username | The username for authentication. When this option is included in a connection
URI, you must percent-encode it. Data Type: string Connection URI:  username=my+user | |||
| パスワード | The password for authentication. When this option is included in a connection
URI, you must percent-encode it. Data Type: string Connection URI:  password=strong+password | 
読み取り操作と書込み操作
さまざまなタイプの MongoDB 配置への接続の詳細については、 「 接続ターゲットの選択 」ガイドを参照してください。
| 接続オプション | 説明 | |||
|---|---|---|---|---|
| replicaSet | Specifies the name of the replica set to connect to. Data Type: string Connection URI:  replicaSet=myRS | |||
| directConnection | Whether to connect only to the primary member of the replica set. Data Type: boolean Default:  falseMongoClientSettings: Connection URI:  directConnection=true | |||
| readPreference | Specifies the client's read preference. For more information,
see Read Preference in the
Server manual. Data Type: string Default:  primaryMongoClientSettings:  readPreference(ReadPreference.primary())Connection URI:  readPreference=primary | |||
| ReadConcern | Specifies the client's read concern. For more information, see
Read Concern in the Server
manual. Data Type: string MongoClientSettings:  readConcern(ReadConcern.MAJORITY)Connection URI:  readConcern=majority | |||
| writeConcern | Specifies the client's write concern. For more information, see
Write Concern in the
Server manual. Data Type: string MongoClientSettings:  writeConcern(WriteConcern.MAJORITY)Connection URI:  writeConcern=majority | |||
| localThresholdMS | The latency window for a replica-set member's eligibility. If a member's
round trip ping takes longer than the fastest server's round-trip ping
time plus this value, the server isn't eligible for selection. Data Type: integer Default:  15MongoClientSettings: Connection URI:  localThresholdMS=35 | 
詳細情報
接続オプションの完全なリストを表示するには、サーバー マニュアルの「接続文字列」を参照してください。
API ドキュメント
このガイドで言及されているクラスとメソッドについて詳しくは、次の API ドキュメントを参照してください。