接続オプションの指定
Overview
このセクションでは、PyMongo で使用できる MongoDB の接続オプションと認証オプションについて説明します。 接続 URI または MongoClient
コンストラクターへの引数のいずれかを使用して接続を構成できます。
接続 URI の使用
MongoClient
コンストラクターに接続 URI を渡す場合は、接続オプションを <name>=<value>
ペアとして文字列に含めることができます。次の例では、接続 URI に、値が 60000
の connectTimeoutMS
オプションと、値が true
の tls
オプションが含まれています。
uri = "mongodb://<hostname>:<port>/?connectTimeoutMS=60000&tls=true" client = pymongo.MongoClient(uri)
を使用する MongoClient
接続 URI に接続オプションを含める代わりに、引数としてMongoClient
コンストラクターに接続オプションを渡すことができます。 このように接続を構成すると、実行時に設定を変更しやすくなり、コンパイル中にエラーを検出しやすくなります。 次の例は、 MongoClient
コンストラクターを使用して接続オプションを設定する方法を示しています。
uri = "mongodb://<hostname>:<port>" client = pymongo.MongoClient(uri, connectTimeoutMS=60000, tls=True)
接続オプション
次のセクションでは、PyMongo で使用できる接続オプションについて説明します。 MongoClient
パラメータが 接続string内の複数のオプションにマップされている場合、接続 URI の例には関連するすべてのオプションが表示されます。
ネットワーク圧縮
接続オプション | 説明 |
---|---|
| 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: str Default: None MongoClient Example: compressors = "snappy,zstd,zlib" Connection URI Example: compressors=snappy,zstd,zlib |
| 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: int Default: -1 MongoClient Example: zlibCompressionLevel = 3 Connection URI Example: zlibCompressionLevel=3 |
タイムアウト
接続オプション | 説明 |
---|---|
| The number of milliseconds each driver operation must complete within. If an operation doesn't finish in the specified time, PyMongo raises a timeout exception. For more information, see Limit Server Execution Time. Data Type: int Default: None MongoClient Example: timeoutMS = 10000 Connection URI Example: timeoutMs=10000 |
サーバーの選択
接続オプション | 説明 |
---|---|
| A user-defined Python function called by PyMongo to choose the server to run an operation against. For more information, see Data Type: callable Default: None MongoClient Example: server_selector = your_function Connection URI Example: N/A |
接続プール
接続プール とはオープンデータベース接続のキャッシュであり、 PyMongoによって維持されます。アプリケーションがMongoDBへの接続を要求すると、 PyMongo はプールから接続を取得し、操作を実行し、再利用するためにプールに接続を返します。接続プールは、アプリケーションのレイテンシとPyMongo が新しい接続を作成する必要がある回数を削減するのに役立ちます。
接続プールの詳細については、 MongoDB Serverマニュアルの「 接続プールの概要」を参照してください。
設定 | 説明 |
---|---|
| The time that PyMongo waits when establishing a new
connection before timing out. Data Type: int Default: 20000 MongoClient Example: connectTimeoutMS = 40000 Connection URI Example: connectTimeoutMS=40000 |
| The maximum number of connections that each pool can establish concurrently.
If this limit is reached, further requests wait until a connection is established
or another in-use connection is checked back into the pool. Data Type: int Default: 2 MongoClient Example: maxConnecting = 3 Connection URI Example: maxConnecting=3 |
| The maximum time that a connection can remain idle in the pool. When a connection
exceeds this limit, PyMongo closes the connection and removes it from
the pool. Data Type: int Default: None (no limit)MongoClient Example: maxIdleTimeMS = 60000 Connection URI Example: maxIdleTimeMS=60000 |
| The maximum number of concurrent connections that the pool maintains.
If the maximum pool size is reached, further requests wait until a connection
becomes available. Data Type: int Default: 100 MongoClient Example: maxPoolSize = 150 Connection URI Example: maxPoolSize=150 |
| The minimum number of concurrent connections that the pool maintains. If
the number of open connections falls below this value due to network errors,
PyMongo attempts to create new connections to maintain this minimum. Data Type: int Default: 0 MongoClient Example: minPoolSize = 3 Connection URI Example: minPoolSize=3 |
| The length of time that PyMongo waits for a response from the server
before timing out. Data Type: int Default: None (no timeout)MongoClient Example: socketTimeoutMS = 100000 Connection URI Example: socketTimeoutMS=100000 |
| How long a thread waits for a connection to become available in the connection pool
before timing out. Data Type: int Default: None (no timeout)MongoClient Example: waitQueueTimeoutMS = 100000 Connection URI Example: waitQueueTimeoutMS=100000 |
認証
接続オプション | 説明 |
---|---|
| The mechanism PyMongo uses to authenticate the application. Valid options are defined in MECHANISMS. Data Type: str Default: "SCRAM-SHA-256" when connecting to MongoDB v4.0 or later."SCRAM-SHA-1" when connecting to MongoDB v3.0 through v3.13.MongoClient Example: authMechanism = "MONGODB-X509" Connection URI Example: authMechanism=MONGODB-X509 |
| Options specific to the authentication mechanism. Not needed for all authentication mechanisms. Data Type: str Default: "" MongoClient Example: authMechanismProperties = "AWS_SESSION_TOKEN:12345" Connection URI Example: authMechanismProperties=AWS_SESSION_TOKEN:12435 |
| The database to authenticate against. Data Type: str Default: The database in the connection URI, or "admin" if none is providedMongoClient Example: authSource = "admin" Connection URI Example: authSource=admin |
| The username for authentication. When this option is included in a connection URI, you must percent-escape it. Data Type: str Default: "" MongoClient Example: username = "my user" Connection URI Example: username=my+user |
| The password for authentication. When this option is included in a connection URI, you must percent-escape it. Data Type: str Default: "" MongoClient Example: password = "strong password" Connection URI Example: password=strong+password |
このセクションの 接続オプションの詳細については、「認証メカニズム 」を参照してください。
読み取り操作と書込み操作
接続オプション | 説明 |
---|---|
| Specifies the name of the replica set to connect to. Data Type: str Default: null MongoClient Example: replicaSet='replicaSetName' Connection URI Example: replicaSet=replicaSetName |
| Whether to connect only to the primary member of the replica set. Data Type: bool Default: False MongoClient Example: directConnection=True Connection URI Example: directConnection=true |
| Specifies the client's read-preference settings. Data Type: read_preferences Default: ReadPreference.Primary MongoClient Example: readPreference=ReadPreference.SECONDARY_PREFERRED Connection URI Example: readPreference=secondaryPreferred |
| Specifies the client's read-concern settings. For more information, see /reference/read-concern/. Data Type: str Default: None MongoClient Example: readConcern="majority" Connection URI Example: readConcern=majority |
| Specifies the client's write-concern settings. For more information, see
/reference/write-concern/. Data Type: str Default: None MongoClient Example: writeConcern="majority" Connection URI Example: writeConcern=majority |
| The latency window for a replica-set members 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: read_preferences Default: int MongoClient Example: localThresholdMS=35 Connection URI Example: localThresholdMS=35 |
| Specifies whether the client retries supported read operations. For more
information, see Retryable Reads in the MongoDB Server
manual. Data Type: bool Default: True MongoClient Example: retryReads=False Connection URI Example: retryReads=false |
| Specifies whether the client retries supported write operations. For more
information, see Retryable Writes in the MongoDB Server
manual. Data Type: bool Default: True MongoClient Example: retryWrites=False Connection URI Example: retryWrites=false |
このセクションの接続オプションの詳細については、「 データベースとコレクション 」を参照してください。