ANNOUNCEMENT: Voyage AI joins MongoDB to power more accurate and trustworthy AI applications on Atlas.
Learn more
Docs Menu

接続 URI オプションの指定

このセクションでは、 Java Reactive Streams ドライバーの接続 URI のパラメーターとして渡すことができるMongoDBの接続オプションと認証オプションについて説明します。

MongoClient コンストラクターに接続 URI を渡す場合は、接続オプションを <name>=<value> ペアとして文字列に含めることができます。次の例では、接続 URI に、値が 60000connectTimeoutMS オプションと、値が truetls オプションが含まれています。

public class MongoConnection {
public static void main(String[] args) {
String uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true";
try (MongoClient client = MongoClients.create(uri)) {
// Use client here
}
}
}

次のセクションでは、 Java Reactive Streams ドライバーで使用できる接続オプションについて説明します。 これらのオプションを接続 URI のパラメーターとして に渡し、クライアントの動作を指定できます。

接続オプション
説明

authMechanism

Sets the mechanism the Java Reactive Streams driver uses to authenticate the
application. Valid options are defined in the Class ConnectionString API documentation.

Data Type: String
Default: "SCRAM-SHA-256" when connecting to MongoDB v4.0 or later.
"SCRAM-SHA-1" when connecting to MongoDB v3.0 through v3.13.
Connection URI Example: authMechanism=MONGODB-X509

authMechanismProperties

Sets options specific to the authentication mechanism.

Data Type: String
Default: ""
Connection URI Example: authMechanismProperties=AWS_SESSION_TOKEN:12435

authSource

Sets the source of authentication credentials. The source is typically a
database. This option is only respected when authMechanism is set
to MONGO-CR.

Data Type: String
Default: The database in the connection URI, or "admin" if none is provided
Connection URI Example: authSource=admin

username

Sets the username for authentication. When this option is included in a connection
URI, you must percent-escape it.

Data Type: String
Default: ""
Connection URI Example: username=my+user

パスワード

Sets the password for authentication. When this option is included in a connection
URI, you must percent-escape it.

Data Type: String
Default: ""
Connection URI Example: password=strong+password
接続オプション
説明

connectTimeoutMS

Sets the number of milliseconds the connection can take before timing out. If an
operation doesn't finish in the specified time, the Java Reactive Streams driver raises a timeout exception.

Data Type: Integer
Default: null
Connection URI Example: connectTimeoutMS=6000

maxIdleTimeMS

Sets the maximum idle time in milliseconds of a pooled connection. If an
operation doesn't finish in the specified time, the Java Reactive Streams driver
closes the connection.

Data Type: Integer
Default: null
Connection URI Example: maxIdleTimeMS=6000

maxLifeTimeMS

Sets the maximum lifetime in milliseconds of a pooled connection. If an
operation doesn't finish in the specified time, the Java Reactive Streams driver
closes the connection.

Data Type: Integer
Default: null
Connection URI Example: maxLifeTimeMS=6000

socketTimeoutMS

Sets the number of milliseconds a recieve on a socket can take before timing out. If an
operation doesn't finish in the specified time, the Java Reactive Streams driver raises a timeout exception.

Data Type: Integer
Default: null
Connection URI Example: socketTimeoutMS=6000

ssl

Sets whether to connect using TLS/SSL.

Data Type: boolean
Default: null
Connection URI Example: ssl=true

sslInvalidHostNameAllowed

Allows invalid host names for TLS/SSL connections.

Data Type: boolean
Default: null
Connection URI Example: sslInvalidHostNameAllowed=true

tls

Sets whether to connect using TLS. Supersedes the ssl option.

Data Type: boolean
Default: null
Connection URI Example: tls=true

tlsAllowInvalidHostnames

Allows invalid host names for TLS connections. Supersedes the
sslInvalidHostNameAllowed option.

Data Type: boolean
Default: null
Connection URI Example: tlsAllowInvalidHostnames=true

tlsInsecure

Enables insecure TLS connections when connecting using TLS.

Data Type: boolean
Default: null
Connection URI Example: tlsInsecure=true
接続オプション
説明

maxConnecting

Sets the maximum number of connections a pool can establish concurrently.

Data Type: Integer
Default: null
Connection URI Example: maxConnecting=5

maxPoolSize

Sets the maximum number of connections in the connection pool.

Data Type: Integer
Default: null
Connection URI Example: maxPoolSize=5

minPoolSize

Sets the minimum number of connections in the connection pool.

Data Type: Integer
Default: null
Connection URI Example: minPoolSize=3

waitQueueTimeoutMS

Sets the maximum amount of time to wait in milliseconds before either
an in-use connection becomes available or a connection is created and
starts becoming established.

Data Type: Integer
Default: null
Connection URI Example: waitQueueTimeoutMS=6000
接続オプション
説明

compressors

Sets preferred compression types, in order, for wire-protocol messages sent to
or received from the server. Compression types must be passed in as a
comma-separated list. The driver uses the first of these compression types
that the server supports.

Data Type: String
Default: null
Connection URI Example: compressors=snappy,zstd,zlib

zlibCompressionLevel

Sets the compression level for zlib to use. This option accepts
an integer value between -1 and 9:

- -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
Connection URI Example: zLibCompressionLevel=3
接続オプション
説明

directConnection

Sets whether the driver connects directly to the host.

Data Type: boolean
Default: null
Connection URI Example: directConnection=true

loadBalanced

Sets whether or not the driver is connecting to MongoDB using a load
balancer. If true, the driver assumes that it is connecting through a
load balancer.

Data Type: boolean
Default: null
Connection URI Example: directConnection=true

retryWrites

Sets whether or not the driver retries supported write operations if
they fail due to a network error.

Data Type: boolean
Default: True
Connection URI Example: retryWrites=true

retryReads

Sets whether or not the driver retries supported read operations if
they fail due to a network error.

Data Type: boolean
Default: True
Connection URI Example: retryReads=true

uuidRepresentation

Sets the mechanism the Java Reactive Streams driver uses to encode instances of
UUID. See the Uuid Representation API documentation for a list of all
valid options.

Data Type: String
Default: unspecified
Connection URI Example: uuidRepresentation=unspecified
接続オプション
説明

proxyHost

Establishes the SOCKS5 proxy host to establish a connection through.
The value of the proxyHost can be a valid IPv4 address, IPv6
address, or a domain name.
Required if either proxyPassword, proxyUsername or proxyPort are specified.

Data Type: String
Default: null
Connection URI Example: proxyHost=example.com

proxyPort

Sets the port number for the SOCKS5 proxy server. Must be a non-negative integer.

Data Type: Integer
Default: null
Connection URI Example: proxyPort=27

proxyUsername

Sets the username for authenticating with the proxy server. Required if proxyPassword is specified.

Data Type: String
Default: null
Connection URI Example: proxyUsername=example

proxyPassword

Sets the password for authenticating with the proxy server. Required if
proxyPassword is specified.

Data Type: String
Default: null
Connection URI Example: proxyPassword=password
接続オプション
説明

readPreference

Sets the read preference for this connection. Enumerated values are:
primary, primaryPreferred, secondary, secondaryPreferred, and nearest.

Data Type: Enum
Default: primary
Connection URI Example: readPreference.primary

readPreferenceTags

Specifies a tag set as a comma-separated list of
colon-separated key-value pairs, e.g. dc:ny,rack:1.
Spaces are stripped from beginning and end of all keys and values. To
specify a list of tag sets, use multiple readPreferenceTags, separated
by semicolons. To learn more about readPreferenceTags, see the Class ConnectionString API documentation.

Data Type: String
Default: null
Connection URI Example: readPreferenceTags=dc:ny

maxStalenessSeconds

Sets the maximum staleness in seconds. To learn more about staleness,
see the Class ConnectionString API documentation.

Data Type: Integer
Default: null
Connection URI Example: maxStalenessSeconds=60
接続オプション
説明

replicaSet

Specifies the name of the replica set to connect to.

Data Type: String
Default: null
Connection URI Example: replicaSet=replicaSet
接続オプション
説明

appName

Sets the logical name of the application.
The application name may be used by the client to identify the
application to the server,
for use in server logs, slow query logs, and profile collection.

Data Type: String
Default: null
Connection URI Example: appName=name

heartbeatFrequencyMS

The frequency in milliseconds of the driver determining the current state of each server in the cluster.

Data Type: Integer
Default: null
Connection URI Example: heartbeatFrequencyMS=6000

localThresholdMS

Sets a ping time threshold for choosing among multiple MongoDB servers
to send a request. When this value is set, the driver will only send
that request to a server whose
ping time is less than or equal to the server with the fastest ping time plus the local threshold.

Data Type: Integer
Default: null
Connection URI Example: localThresholdMS=6000

serverMonitoringMode

Sets the server monitoring mode, which defines the monitoring protocol to use. The enumerated values are: stream, poll, and auto.

Data Type: Enum
Default: auto
Connection URI Example: serverMonitoringMode.auto

serverSelectionTimeoutMS

Sets how long the driver will wait for server selection to succeed before throwing an exception.

Data Type: Integer
Default: null
Connection URI Example: serverSelectionTimeoutMS=6000
接続オプション
説明

srvServiceName

Sets the SRV service name. See
API documentation to learn more.

Data Type: String
Default: "mongodb"
Connection URI Example: srvServiceName=mongodb

srvMaxHosts

Sets the maximum number of hosts from the SRV record to connect to.

Data Type: Integer
Default: null
Connection URI Example: srvMaxHosts=5
接続オプション
説明

安全

Sets whether the driver ensures that all writes are acknowledged by the
MongoDB server, or else throws an exception. If false, the driver
does not ensure that all writes are acknowledged by the MongoDB server.

Data Type: boolean
Default: null
Connection URI Example: safe=true

journal

Sets whether the driver waits for the server to group commit to the
journal file on disk.

Data Type: boolean
Default: null
Connection URI Example: journal=true

w

If set, the driver adds { w : wValue } to all write commands. If set,
implies that safe=True.

Data Type: Integer or String
Default: null
Connection URI Example: w=60

wTimeoutMS

If set, The driver adds { wtimeout : ms } to all write commands. If
set, implies safe=true. This option is used in combination with w.

Data Type: Integer
Default: null
Connection URI Example: wtimeoutMS=6000

このセクションの接続オプションの詳細については、 「データベースとコレクションのガイド」 を参照してください。