Client for a MongoDB instance, a replica set, or a set of mongoses. .. warning:: Starting in PyMongo 4.0, ``directConnection`` now has a default value of False instead of None. For more details, see the relevant section of the PyMongo 4.x migration guide: :ref:
(
self,
host: Optional[Union[str, Sequence[str]]] = None,
port: Optional[int] = None,
document_class: Optional[Type[_DocumentType]] = None,
tz_aware: Optional[bool] = None,
connect: Optional[bool] = None,
type_registry: Optional[TypeRegistry] = None,
**kwargs: Any,
)
| 183 | _clients: weakref.WeakValueDictionary = weakref.WeakValueDictionary() # type: ignore[type-arg] |
| 184 | |
| 185 | def __init__( |
| 186 | self, |
| 187 | host: Optional[Union[str, Sequence[str]]] = None, |
| 188 | port: Optional[int] = None, |
| 189 | document_class: Optional[Type[_DocumentType]] = None, |
| 190 | tz_aware: Optional[bool] = None, |
| 191 | connect: Optional[bool] = None, |
| 192 | type_registry: Optional[TypeRegistry] = None, |
| 193 | **kwargs: Any, |
| 194 | ) -> None: |
| 195 | """Client for a MongoDB instance, a replica set, or a set of mongoses. |
| 196 | |
| 197 | .. warning:: Starting in PyMongo 4.0, ``directConnection`` now has a default value of |
| 198 | False instead of None. |
| 199 | For more details, see the relevant section of the PyMongo 4.x migration guide: |
| 200 | :ref:`pymongo4-migration-direct-connection`. |
| 201 | |
| 202 | The client object is thread-safe and has connection-pooling built in. |
| 203 | If an operation fails because of a network error, |
| 204 | :class:`~pymongo.errors.ConnectionFailure` is raised and the client |
| 205 | reconnects in the background. Application code should handle this |
| 206 | exception (recognizing that the operation failed) and then continue to |
| 207 | execute. |
| 208 | |
| 209 | Best practice is to call :meth:`AsyncMongoClient.close` when the client is no longer needed, |
| 210 | or use the client in a with statement:: |
| 211 | |
| 212 | async with AsyncMongoClient(url) as client: |
| 213 | # Use client here. |
| 214 | |
| 215 | The `host` parameter can be a full `mongodb URI |
| 216 | <https://dochub.mongodb.org/core/connections>`_, in addition to |
| 217 | a simple hostname. It can also be a list of hostnames but no more |
| 218 | than one URI. Any port specified in the host string(s) will override |
| 219 | the `port` parameter. For username and |
| 220 | passwords reserved characters like ':', '/', '+' and '@' must be |
| 221 | percent encoded following RFC 2396:: |
| 222 | |
| 223 | from urllib.parse import quote_plus |
| 224 | |
| 225 | uri = "mongodb://%s:%s@%s" % ( |
| 226 | quote_plus(user), quote_plus(password), host) |
| 227 | client = AsyncMongoClient(uri) |
| 228 | |
| 229 | Unix domain sockets are also supported. The socket path must be percent |
| 230 | encoded in the URI:: |
| 231 | |
| 232 | uri = "mongodb://%s:%s@%s" % ( |
| 233 | quote_plus(user), quote_plus(password), quote_plus(socket_path)) |
| 234 | client = AsyncMongoClient(uri) |
| 235 | |
| 236 | But not when passed as a simple hostname:: |
| 237 | |
| 238 | client = AsyncMongoClient('/tmp/mongodb-27017.sock') |
| 239 | |
| 240 | Starting with version 3.6, PyMongo supports mongodb+srv:// URIs. The |
| 241 | URI must include one, and only one, hostname. The hostname will be |
| 242 | resolved to one or more DNS `SRV records |
nothing calls this directly
no test coverage detected