Docs 菜单
Docs 主页
/ / /
Kotlin Sync 驱动程序
/

指定连接选项

在此页面上

  • Overview
  • 在连接 URI 中设置选项
  • 在 MongoClientSettings 中设置选项
  • 连接选项
  • 网络压缩
  • 超时
  • 服务器选择
  • 身份验证
  • 读取和写入操作
  • 更多信息
  • API 文档

本节介绍Kotlin Sync驾驶员中可用的MongoDB连接和身份验证选项。 您可以通过在连接 URI 中或在 MongoClientSettings实例中设置选项来配置连接。

如果将连接 URI 传递给 MongoClient.create() 方法,则可以将连接选项作为 <name>=<value> 对包含在string中。 在以下示例中,连接 URIconnectTimeoutMS 包含值为 的60000 选项和值为tlstrue 选项:

val uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true"
val mongoClient = MongoClient.create(uri)

您可以使用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 -1 and 9, 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: -1
MongoClientSettings: 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: 10000
MongoClientSettings:
applyToSocketSettings{ builder ->
builder.connectTimeout(10, TimeUnit.SECONDS)
}
Connection URI: timeoutMs=10000
socketTimeoutMS
The time in milliseconds to attempt a send or receive on a connection before the attempt times out.

Data Type: integer
Default: no timeout
MongoClientSettings:
applyToSocketSettings{ builder ->
builder.readTimeout(5, TimeUnit.SECONDS)
}
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: 30000
MongoClientSettings:
applyToClusterSettings{ builder ->
builder.serverSelectionTimeout(30, TimeUnit.SECONDS)
}
Connection URI: serverSelectionTimeoutMS=30000
连接选项
说明
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 later
MongoClientSettings:
credential(
MongoCredential.createScramSha256Credential(...)
)
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
用户名
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: false
MongoClientSettings:
applyToClusterSettings{ builder ->
builder.mode(ClusterConnectionMode.SINGLE)
}
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: primary
MongoClientSettings: readPreference(ReadPreference.primary())
Connection URI: readPreference=primary
事务外的
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: 15
MongoClientSettings:
applyToClusterSettings{ builder ->
builder.localThreshold(35, TimeUnit.MILLISECONDS)
}
Connection URI: localThresholdMS=35

要查看连接选项的完整列表,请参阅服务器手册中的连接字符串

要学习;了解有关本指南中提到的类和方法的更多信息,请参阅以下API文档:

后退

选择连接目标