Docs 菜单
Docs 主页
/ / /
pymongo
/

指定连接选项

在此页面上

  • Overview
  • 连接选项

本节介绍 PyMongo 中可用的 MongoDB 连接和身份验证选项。 您可以使用连接 URI 或 MongoClient构造函数的参数来配置连接。

如果将连接 URI 传递给 MongoClient 构造函数,则可以将连接选项作为 <name>=<value> 对包含在字符串中。在以下示例中,连接 URI 包含值为 60000connectTimeoutMS 选项和值为 truetls 选项:

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

您可以将连接选项作为参数传递给MongoClient构造函数,而不是将它们包含在连接 URI 中。 以这种方式配置连接可以更轻松地在运行时更改设置,并帮助您在编译期间捕获错误。 以下示例演示如何使用MongoClient构造函数设置连接选项:

uri = "mongodb://<hostname>:<port>"
client = pymongo.MongoClient(uri, connectTimeoutMS=60000, tls=True)

以下部分描述了 PyMongo 中可用的连接选项。 如果 MongoClient 参数映射到连接string中的多个选项,则连接 URI 示例会显示所有相关选项。

连接选项
说明
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: str
Default: None
MongoClient Example: compressors = "snappy,zstd,zlib"
Connection URI Example: compressors=snappy,zstd,zlib
zlibCompressionLevel
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
连接选项
说明
timeoutMS
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
连接选项
说明
server_selector
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
连接选项
说明
authMechanism
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
authMechanismProperties
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
authSource
The database to authenticate against.

Data Type: str
Default: The database in the connection URI, or "admin" if none is provided
MongoClient 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

有关本节中连接选项的更多信息,请参阅身份验证机制。

连接选项
说明
replicaSet
Specifies the name of the replica set to connect to.

Data Type: str
Default: null
MongoClient Example: replicaSet='replicaSetName'
Connection URI Example: replicaSet=replicaSetName
directConnection
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
readPreference
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
writeConcern
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
localThresholdMS
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

有关本节中连接选项的更多信息,请参阅数据库和集合。

后退

选择连接目标

在此页面上