mongo_client – Tools for connecting to MongoDB

Tools for connecting to MongoDB.

See also

High Availability and PyMongo for examples of connecting to replica sets or sets of mongos servers.

To get a AsyncDatabase instance from a AsyncMongoClient use either dictionary-style or attribute-style access:

>>> from pymongo import AsyncMongoClient
>>> c = AsyncMongoClient()
>>> c.test_database
AsyncDatabase(AsyncMongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'test_database')
>>> c["test-database"]
AsyncDatabase(AsyncMongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'test-database')
class pymongo.asynchronous.mongo_client.AsyncMongoClient(host='localhost', port=27017, document_class=dict, tz_aware=False, connect=True, **kwargs)

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: directConnection defaults to False.

The client object is thread-safe and has connection-pooling built in. If an operation fails because of a network error, ConnectionFailure is raised and the client reconnects in the background. Application code should handle this exception (recognizing that the operation failed) and then continue to execute.

The host parameter can be a full mongodb URI, in addition to a simple hostname. It can also be a list of hostnames but no more than one URI. Any port specified in the host string(s) will override the port parameter. For username and passwords reserved characters like ‘:’, ‘/’, ‘+’ and ‘@’ must be percent encoded following RFC 2396:

from urllib.parse import quote_plus

uri = "mongodb://%s:%s@%s" % (
    quote_plus(user), quote_plus(password), host)
client = AsyncMongoClient(uri)

Unix domain sockets are also supported. The socket path must be percent encoded in the URI:

uri = "mongodb://%s:%s@%s" % (
    quote_plus(user), quote_plus(password), quote_plus(socket_path))
client = AsyncMongoClient(uri)

But not when passed as a simple hostname:

client = AsyncMongoClient('/tmp/mongodb-27017.sock')

Starting with version 3.6, PyMongo supports mongodb+srv:// URIs. The URI must include one, and only one, hostname. The hostname will be resolved to one or more DNS SRV records which will be used as the seed list for connecting to the MongoDB deployment. When using SRV URIs, the authSource and replicaSet configuration options can be specified using TXT records. See the Initial DNS Seedlist Discovery spec for more details. Note that the use of SRV URIs implicitly enables TLS support. Pass tls=false in the URI to override.

Note

AsyncMongoClient creation will block waiting for answers from DNS when mongodb+srv:// URIs are used.

Note

Starting with version 3.0 the AsyncMongoClient constructor no longer blocks while connecting to the server or servers, and it no longer raises ConnectionFailure if they are unavailable, nor ConfigurationError if the user’s credentials are wrong. Instead, the constructor returns immediately and launches the connection process on background threads. You can check if the server is available like this:

from pymongo.errors import ConnectionFailure
client = AsyncMongoClient()
try:
    # The ping command is cheap and does not require auth.
    client.admin.command('ping')
except ConnectionFailure:
    print("Server not available")

Warning

When using PyMongo in a multiprocessing context, please read Using PyMongo with Multiprocessing first.

Note

Many of the following options can be passed using a MongoDB URI or keyword parameters. If the same option is passed in a URI and as a keyword parameter the keyword parameter takes precedence.

Parameters:
  • host (Optional[Union[str, Sequence[str]]]) – hostname or IP address or Unix domain socket path of a single mongod or mongos instance to connect to, or a mongodb URI, or a list of hostnames (but no more than one mongodb URI). If host is an IPv6 literal it must be enclosed in ‘[’ and ‘]’ characters following the RFC2732 URL syntax (e.g. ‘[::1]’ for localhost). Multihomed and round robin DNS addresses are not supported.

  • port (Optional[int]) – port number on which to connect

  • document_class (Optional[Type[_DocumentType]]) – default class to use for documents returned from queries on this client

  • tz_aware (Optional[bool]) – if True, datetime instances returned as values in a document by this AsyncMongoClient will be timezone aware (otherwise they will be naive)

  • connect (Optional[bool]) – Not supported by AsyncMongoClient.

  • type_registry (Optional[TypeRegistry]) – instance of TypeRegistry to enable encoding and decoding of custom types.

  • datetime_conversion

    Specifies how UTC datetimes should be decoded

    within BSON. Valid options include ‘datetime_ms’ to return as a DatetimeMS, ‘datetime’ to return as a datetime.datetime and raising a ValueError for out-of-range values, ‘datetime_auto’ to return DatetimeMS objects when the underlying datetime is out-of-range and ‘datetime_clamp’ to clamp to the minimum and maximum possible datetimes. Defaults to ‘datetime’. See Handling out of range datetimes for details.

    Other optional parameters can be passed as keyword arguments:
    • directConnection (optional): if True, forces this client to

      connect directly to the specified MongoDB host as a standalone. If false, the client connects to the entire replica set of which the given MongoDB host(s) is a part. If this is True and a mongodb+srv:// URI or a URI containing multiple seeds is provided, an exception will be raised.

    • maxPoolSize (optional): The maximum allowable number of concurrent connections to each connected server. Requests to a server will block if there are maxPoolSize outstanding connections to the requested server. Defaults to 100. Can be either 0 or None, in which case there is no limit on the number of concurrent connections.

    • minPoolSize (optional): The minimum required number of concurrent connections that the pool will maintain to each connected server. Default is 0.

    • maxIdleTimeMS (optional): The maximum number of milliseconds that a connection can remain idle in the pool before being removed and replaced. Defaults to None (no limit).

    • maxConnecting (optional): The maximum number of connections that each pool can establish concurrently. Defaults to 2.

    • timeoutMS: (integer or None) Controls how long (in milliseconds) the driver will wait when executing an operation (including retry attempts) before raising a timeout error. 0 or None means no timeout.

    • socketTimeoutMS: (integer or None) Controls how long (in milliseconds) the driver will wait for a response after sending an ordinary (non-monitoring) database operation before concluding that a network error has occurred. 0 or None means no timeout. Defaults to None (no timeout).

    • connectTimeoutMS: (integer or None) Controls how long (in milliseconds) the driver will wait during server monitoring when connecting a new socket to a server before concluding the server is unavailable. 0 or None means no timeout. Defaults to 20000 (20 seconds).

    • server_selector: (callable or None) Optional, user-provided function that augments server selection rules. The function should accept as an argument a list of ServerDescription objects and return a list of server descriptions that should be considered suitable for the desired operation.

    • serverSelectionTimeoutMS: (integer) Controls how long (in milliseconds) the driver will wait to find an available, appropriate server to carry out a database operation; while it is waiting, multiple server monitoring operations may be carried out, each controlled by connectTimeoutMS. Defaults to 30000 (30 seconds).

    • waitQueueTimeoutMS: (integer or None) How long (in milliseconds) a thread will wait for a socket from the pool if the pool has no free sockets. Defaults to None (no timeout).

    • heartbeatFrequencyMS: (optional) The number of milliseconds between periodic server checks, or None to accept the default frequency of 10 seconds.

    • serverMonitoringMode: (optional) The server monitoring mode to use. Valid values are the strings: “auto”, “stream”, “poll”. Defaults to “auto”.

    • appname: (string or None) The name of the application that created this AsyncMongoClient instance. The server will log this value upon establishing each connection. It is also recorded in the slow query log and profile collections.

    • driver: (pair or None) A driver implemented on top of PyMongo can pass a DriverInfo to add its name, version, and platform to the message printed in the server log when establishing a connection.

    • event_listeners: a list or tuple of event listeners. See monitoring for details.

    • retryWrites: (boolean) Whether supported write operations executed within this AsyncMongoClient will be retried once after a network error. Defaults to True. The supported write operations are:

      Unsupported write operations include, but are not limited to, aggregate() using the $out pipeline operator and any operation with an unacknowledged write concern (e.g. {w: 0})). See https://github.com/mongodb/specifications/blob/master/source/retryable-writes/retryable-writes.rst

    • retryReads: (boolean) Whether supported read operations executed within this AsyncMongoClient will be retried once after a network error. Defaults to True. The supported read operations are: find(), find_one(), aggregate() without $out, distinct(), count(), estimated_document_count(), count_documents(), pymongo.asynchronous.collection.AsyncCollection.watch(), list_indexes(), pymongo.asynchronous.database.AsyncDatabase.watch(), list_collections(), pymongo.asynchronous.mongo_client.AsyncMongoClient.watch(), and list_databases().

      Unsupported read operations include, but are not limited to command() and any getMore operation on a cursor.

      Enabling retryable reads makes applications more resilient to transient errors such as network failures, database upgrades, and replica set failovers. For an exact definition of which errors trigger a retry, see the retryable reads specification.

    • compressors: Comma separated list of compressors for wire protocol compression. The list is used to negotiate a compressor with the server. Currently supported options are “snappy”, “zlib” and “zstd”. Support for snappy requires the python-snappy package. zlib support requires the Python standard library zlib module. zstd requires the zstandard package. By default no compression is used. Compression support must also be enabled on the server. MongoDB 3.6+ supports snappy and zlib compression. MongoDB 4.2+ adds support for zstd. See Network Compression for details.

    • zlibCompressionLevel: (int) The zlib compression level to use when zlib is used as the wire protocol compressor. Supported values are -1 through 9. -1 tells the zlib library to use its default compression level (usually 6). 0 means no compression. 1 is best speed. 9 is best compression. Defaults to -1.

    • uuidRepresentation: The BSON representation to use when encoding from and decoding to instances of UUID. Valid values are the strings: “standard”, “pythonLegacy”, “javaLegacy”, “csharpLegacy”, and “unspecified” (the default). New applications should consider setting this to “standard” for cross language compatibility. See Handling UUID Data for details.

    • unicode_decode_error_handler: The error handler to apply when a Unicode-related error occurs during BSON decoding that would otherwise raise UnicodeDecodeError. Valid options include ‘strict’, ‘replace’, ‘backslashreplace’, ‘surrogateescape’, and ‘ignore’. Defaults to ‘strict’.

    • srvServiceName: (string) The SRV service name to use for “mongodb+srv://” URIs. Defaults to “mongodb”. Use it like so:

      AsyncMongoClient("mongodb+srv://example.com/?srvServiceName=customname")
      
    • srvMaxHosts: (int) limits the number of mongos-like hosts a client will connect to. More specifically, when a “mongodb+srv://” connection string resolves to more than srvMaxHosts number of hosts, the client will randomly choose an srvMaxHosts sized subset of hosts.

    Write Concern options:
    (Only set if passed. No default values.)
    • w: (integer or string) If this is a replica set, write operations will block until they have been replicated to the specified number or tagged set of servers. w=<int> always includes the replica set primary (e.g. w=3 means write to the primary and wait until replicated to two secondaries). Passing w=0 disables write acknowledgement and all other write concern options.

    • wTimeoutMS: DEPRECATED (integer) Used in conjunction with w. Specify a value in milliseconds to control how long to wait for write propagation to complete. If replication does not complete in the given timeframe, a timeout exception is raised. Passing wTimeoutMS=0 will cause write operations to wait indefinitely.

    • journal: If True block until write operations have been committed to the journal. Cannot be used in combination with fsync. Write operations will fail with an exception if this option is used when the server is running without journaling.

    • fsync: If True and the server is running without journaling, blocks until the server has synced all data files to disk. If the server is running with journaling, this acts the same as the j option, blocking until write operations have been committed to the journal. Cannot be used in combination with j.

    Replica set keyword arguments for connecting with a replica set - either directly or via a mongos:
    • replicaSet: (string or None) The name of the replica set to connect to. The driver will verify that all servers it connects to match this name. Implies that the hosts specified are a seed list and the driver should attempt to find all members of the set. Defaults to None.

    Read Preference:
    • readPreference: The replica set read preference for this client. One of primary, primaryPreferred, secondary, secondaryPreferred, or nearest. Defaults to primary.

    • readPreferenceTags: Specifies a tag set as a comma-separated list of colon-separated key-value pairs. For example dc:ny,rack:1. Defaults to None.

    • maxStalenessSeconds: (integer) The maximum estimated length of time a replica set secondary can fall behind the primary in replication before it will no longer be selected for operations. Defaults to -1, meaning no maximum. If maxStalenessSeconds is set, it must be a positive integer greater than or equal to 90 seconds.

    Authentication:
    • username: A string.

    • password: A string.

      Although username and password must be percent-escaped in a MongoDB URI, they must not be percent-escaped when passed as parameters. In this example, both the space and slash special characters are passed as-is:

      AsyncMongoClient(username="user name", password="pass/word")
      
    • authSource: The database to authenticate on. Defaults to the database specified in the URI, if provided, or to “admin”.

    • authMechanism: See MECHANISMS for options. If no mechanism is specified, PyMongo automatically SCRAM-SHA-1 when connected to MongoDB 3.6 and negotiates the mechanism to use (SCRAM-SHA-1 or SCRAM-SHA-256) when connected to MongoDB 4.0+.

    • authMechanismProperties: Used to specify authentication mechanism specific options. To specify the service name for GSSAPI authentication pass authMechanismProperties=’SERVICE_NAME:<service name>’. To specify the session token for MONGODB-AWS authentication pass authMechanismProperties='AWS_SESSION_TOKEN:<session token>'.

    TLS/SSL configuration:
    • tls: (boolean) If True, create the connection to the server using transport layer security. Defaults to False.

    • tlsInsecure: (boolean) Specify whether TLS constraints should be relaxed as much as possible. Setting tlsInsecure=True implies tlsAllowInvalidCertificates=True and tlsAllowInvalidHostnames=True. Defaults to False. Think very carefully before setting this to True as it dramatically reduces the security of TLS.

    • tlsAllowInvalidCertificates: (boolean) If True, continues the TLS handshake regardless of the outcome of the certificate verification process. If this is False, and a value is not provided for tlsCAFile, PyMongo will attempt to load system provided CA certificates. If the python version in use does not support loading system CA certificates then the tlsCAFile parameter must point to a file of CA certificates. tlsAllowInvalidCertificates=False implies tls=True. Defaults to False. Think very carefully before setting this to True as that could make your application vulnerable to on-path attackers.

    • tlsAllowInvalidHostnames: (boolean) If True, disables TLS hostname verification. tlsAllowInvalidHostnames=False implies tls=True. Defaults to False. Think very carefully before setting this to True as that could make your application vulnerable to on-path attackers.

    • tlsCAFile: A file containing a single or a bundle of “certification authority” certificates, which are used to validate certificates passed from the other end of the connection. Implies tls=True. Defaults to None.

    • tlsCertificateKeyFile: A file containing the client certificate and private key. Implies tls=True. Defaults to None.

    • tlsCRLFile: A file containing a PEM or DER formatted certificate revocation list. Implies tls=True. Defaults to None.

    • tlsCertificateKeyFilePassword: The password or passphrase for decrypting the private key in tlsCertificateKeyFile. Only necessary if the private key is encrypted. Defaults to None.

    • tlsDisableOCSPEndpointCheck: (boolean) If True, disables certificate revocation status checking via the OCSP responder specified on the server certificate. tlsDisableOCSPEndpointCheck=False implies tls=True. Defaults to False.

    • ssl: (boolean) Alias for tls.

    Read Concern options:
    (If not set explicitly, this will use the server default)
    • readConcernLevel: (string) The read concern level specifies the level of isolation for read operations. For example, a read operation using a read concern level of majority will only return data that has been written to a majority of nodes. If the level is left unspecified, the server default will be used.

    Client side encryption options:
    (If not set explicitly, client side encryption will not be enabled.)
    Stable API options:
    (If not set explicitly, Stable API will not be enabled.)

  • kwargs (Any)

See also

The MongoDB documentation on connections.

Changed in version 4.5: Added the serverMonitoringMode keyword argument.

Changed in version 4.2: Added the timeoutMS keyword argument.

Changed in version 4.0:

  • Removed the fsync, unlock, is_locked, database_names, and close_cursor methods. See the PyMongo 4 Migration Guide.

  • Removed the waitQueueMultiple and socketKeepAlive keyword arguments.

  • The default for uuidRepresentation was changed from pythonLegacy to unspecified.

  • Added the srvServiceName, maxConnecting, and srvMaxHosts URI and keyword arguments.

Changed in version 3.12: Added the server_api keyword argument. The following keyword arguments were deprecated:

  • ssl_certfile and ssl_keyfile were deprecated in favor of tlsCertificateKeyFile.

Changed in version 3.11: Added the following keyword arguments and URI options:

  • tlsDisableOCSPEndpointCheck

  • directConnection

Changed in version 3.9: Added the retryReads keyword argument and URI option. Added the tlsInsecure keyword argument and URI option. The following keyword arguments and URI options were deprecated:

  • wTimeout was deprecated in favor of wTimeoutMS.

  • j was deprecated in favor of journal.

  • ssl_cert_reqs was deprecated in favor of tlsAllowInvalidCertificates.

  • ssl_match_hostname was deprecated in favor of tlsAllowInvalidHostnames.

  • ssl_ca_certs was deprecated in favor of tlsCAFile.

  • ssl_certfile was deprecated in favor of tlsCertificateKeyFile.

  • ssl_crlfile was deprecated in favor of tlsCRLFile.

  • ssl_pem_passphrase was deprecated in favor of tlsCertificateKeyFilePassword.

Changed in version 3.9: retryWrites now defaults to True.

Changed in version 3.8: Added the server_selector keyword argument. Added the type_registry keyword argument.

Changed in version 3.7: Added the driver keyword argument.

Changed in version 3.6: Added support for mongodb+srv:// URIs. Added the retryWrites keyword argument and URI option.

Changed in version 3.5: Add username and password options. Document the authSource, authMechanism, and authMechanismProperties options. Deprecated the socketKeepAlive keyword argument and URI option. socketKeepAlive now defaults to True.

Changed in version 3.0: AsyncMongoClient is now the one and only client class for a standalone server, mongos, or replica set. It includes the functionality that had been split into MongoReplicaSetClient: it can connect to a replica set, discover all its members, and monitor the set for stepdowns, elections, and reconfigs.

The AsyncMongoClient constructor no longer blocks while connecting to the server or servers, and it no longer raises ConnectionFailure if they are unavailable, nor ConfigurationError if the user’s credentials are wrong. Instead, the constructor returns immediately and launches the connection process on background threads.

Therefore the alive method is removed since it no longer provides meaningful information; even if the client is disconnected, it may discover a server in time to fulfill the next operation.

In PyMongo 2.x, AsyncMongoClient accepted a list of standalone MongoDB servers and used the first it could connect to:

AsyncMongoClient(['host1.com:27017', 'host2.com:27017'])

A list of multiple standalones is no longer supported; if multiple servers are listed they must be members of the same replica set, or mongoses in the same sharded cluster.

The behavior for a list of mongoses is changed from “high availability” to “load balancing”. Before, the client connected to the lowest-latency mongos in the list, and used it until a network error prompted it to re-evaluate all mongoses’ latencies and reconnect to one of them. In PyMongo 3, the client monitors its network latency to all the mongoses continuously, and distributes operations evenly among those with the lowest latency. See mongos Load Balancing for more information.

The connect option is added.

The start_request, in_request, and end_request methods are removed, as well as the auto_start_request option.

The copy_database method is removed, see the copy_database examples for alternatives.

The AsyncMongoClient.disconnect() method is removed; it was a synonym for close().

AsyncMongoClient no longer returns an instance of AsyncDatabase for attribute names with leading underscores. You must use dict-style lookups instead:

client['__my_database__']

Not:

client.__my_database__

Changed in version 4.7: Deprecated parameter wTimeoutMS, use timeout().

Changed in version 4.9: The default value of connect is changed to False when running in a Function-as-a-service environment.

async close()

Cleanup client resources and disconnect from MongoDB.

End all server sessions created by this client by sending one or more endSessions commands.

Close all sockets in the connection pools and stop the monitor threads.

Changed in version 4.0: Once closed, the client cannot be used again and any attempt will raise InvalidOperation.

Changed in version 3.6: End all server sessions created by this client.

Return type:

None

c[db_name] || c.db_name

Get the db_name AsyncDatabase on AsyncMongoClient c.

Raises InvalidName if an invalid database name is used.

topology_description

The description of the connected MongoDB deployment.

>>> client.topology_description
<TopologyDescription id: 605a7b04e76489833a7c6113, topology_type: ReplicaSetWithPrimary, servers: [<ServerDescription ('localhost', 27017) server_type: RSPrimary, rtt: 0.0007973677999995488>, <ServerDescription ('localhost', 27018) server_type: RSSecondary, rtt: 0.0005540556000003249>, <ServerDescription ('localhost', 27019) server_type: RSSecondary, rtt: 0.0010367483999999649>]>
>>> client.topology_description.topology_type_name
'ReplicaSetWithPrimary'

Note that the description is periodically updated in the background but the returned object itself is immutable. Access this property again to get a more recent TopologyDescription.

Returns:

An instance of TopologyDescription.

Added in version 4.0.

address

(host, port) of the current standalone, primary, or mongos, or None.

Accessing address raises InvalidOperation if the client is load-balancing among mongoses, since there is no single address. Use nodes instead.

If the client is not connected, this will block until a connection is established or raise ServerSelectionTimeoutError if no server is available.

Added in version 3.0.

primary

The (host, port) of the current primary of the replica set.

Returns None if this client is not connected to a replica set, there is no primary, or this client was created without the replicaSet option.

Added in version 3.0: AsyncMongoClient gained this property in version 3.0.

secondaries

The secondary members known to this client.

A sequence of (host, port) pairs. Empty if this client is not connected to a replica set, there are no visible secondaries, or this client was created without the replicaSet option.

Added in version 3.0: AsyncMongoClient gained this property in version 3.0.

arbiters

Arbiters in the replica set.

A sequence of (host, port) pairs. Empty if this client is not connected to a replica set, there are no arbiters, or this client was created without the replicaSet option.

is_primary

If this client is connected to a server that can accept writes.

True if the current server is a standalone, mongos, or the primary of a replica set. If the client is not connected, this will block until a connection is established or raise ServerSelectionTimeoutError if no server is available.

is_mongos

If this client is connected to mongos. If the client is not connected, this will block until a connection is established or raise ServerSelectionTimeoutError if no server is available.

nodes

Set of all currently connected servers.

Warning

When connected to a replica set the value of nodes can change over time as AsyncMongoClient’s view of the replica set changes. nodes can also be an empty set when AsyncMongoClient is first instantiated and hasn’t yet connected to any servers, or a network partition causes it to lose connection to all servers.

codec_options

Read only access to the CodecOptions of this instance.

read_preference

Read only access to the read preference of this instance.

Changed in version 3.0: The read_preference attribute is now read only.

write_concern

Read only access to the WriteConcern of this instance.

Changed in version 3.0: The write_concern attribute is now read only.

read_concern

Read only access to the ReadConcern of this instance.

Added in version 3.2.

options

The configuration options for this client.

Returns:

An instance of ClientOptions.

Added in version 4.0.

start_session(causal_consistency=None, default_transaction_options=None, snapshot=False)

Start a logical session.

This method takes the same parameters as SessionOptions. See the client_session module for details and examples.

A AsyncClientSession may only be used with the AsyncMongoClient that started it. AsyncClientSession instances are not thread-safe or fork-safe. They can only be used by one thread or process at a time. A single AsyncClientSession cannot be used to run multiple operations concurrently.

Returns:

An instance of AsyncClientSession.

Parameters:
Return type:

AsyncClientSession

Added in version 3.6.

async list_databases(session=None, comment=None, **kwargs)

Get a cursor over the databases of the connected server.

Parameters:
Returns:

An instance of AsyncCommandCursor.

Return type:

AsyncCommandCursor[dict[str, Any]]

Added in version 3.6.

async list_database_names(session=None, comment=None)

Get a list of the names of all databases on the connected server.

Parameters:
Return type:

list[str]

Changed in version 4.1: Added comment parameter.

Added in version 3.6.

async drop_database(name_or_database, session=None, comment=None)

Drop a database.

Raises TypeError if name_or_database is not an instance of str or AsyncDatabase.

Parameters:
Return type:

None

Changed in version 4.1: Added comment parameter.

Changed in version 3.6: Added session parameter.

Note

The write_concern of this client is automatically applied to this operation.

Changed in version 3.4: Apply this client’s write concern automatically to this operation when connected to MongoDB >= 3.4.

get_default_database(default=None, codec_options=None, read_preference=None, write_concern=None, read_concern=None)

Get the database named in the MongoDB connection URI.

>>> uri = 'mongodb://host/my_database'
>>> client = AsyncMongoClient(uri)
>>> db = client.get_default_database()
>>> assert db.name == 'my_database'
>>> db = client.get_database()
>>> assert db.name == 'my_database'

Useful in scripts where you want to choose which database to use based only on the URI in a configuration file.

Parameters:
Return type:

database.AsyncDatabase[_DocumentType]

Changed in version 4.1: Added comment parameter.

Changed in version 3.8: Undeprecated. Added the default, codec_options, read_preference, write_concern and read_concern parameters.

Changed in version 3.5: Deprecated, use get_database() instead.

get_database(name=None, codec_options=None, read_preference=None, write_concern=None, read_concern=None)

Get a AsyncDatabase with the given name and options.

Useful for creating a AsyncDatabase with different codec options, read preference, and/or write concern from this AsyncMongoClient.

>>> client.read_preference
Primary()
>>> db1 = client.test
>>> db1.read_preference
Primary()
>>> from pymongo import ReadPreference
>>> db2 = client.get_database(
...     'test', read_preference=ReadPreference.SECONDARY)
>>> db2.read_preference
Secondary(tag_sets=None)
Parameters:
Return type:

database.AsyncDatabase[_DocumentType]

Changed in version 3.5: The name parameter is now optional, defaulting to the database named in the MongoDB connection URI.

async server_info(session=None)

Get information about the MongoDB server we’re connected to.

Parameters:

session (AsyncClientSession | None) – a AsyncClientSession.

Return type:

dict[str, Any]

Changed in version 3.6: Added session parameter.

async watch(pipeline=None, full_document=None, resume_after=None, max_await_time_ms=None, batch_size=None, collation=None, start_at_operation_time=None, session=None, start_after=None, comment=None, full_document_before_change=None, show_expanded_events=None)

Watch changes on this cluster.

Performs an aggregation with an implicit initial $changeStream stage and returns a AsyncClusterChangeStream cursor which iterates over changes on all databases on this cluster.

Introduced in MongoDB 4.0.

with client.watch() as stream:
    for change in stream:
        print(change)

The AsyncClusterChangeStream iterable blocks until the next change document is returned or an error is raised. If the next() method encounters a network error when retrieving a batch from the server, it will automatically attempt to recreate the cursor such that no change events are missed. Any error encountered during the resume attempt indicates there may be an outage and will be raised.

try:
    with client.watch([{"$match": {"operationType": "insert"}}]) as stream:
        for insert_change in stream:
            print(insert_change)
except pymongo.errors.PyMongoError:
    # The AsyncChangeStream encountered an unrecoverable error or the
    # resume attempt failed to recreate the cursor.
    logging.error("...")

For a precise description of the resume process see the change streams specification.

Parameters:
  • pipeline (Optional[_Pipeline]) – A list of aggregation pipeline stages to append to an initial $changeStream stage. Not all pipeline stages are valid after a $changeStream stage, see the MongoDB documentation on change streams for the supported stages.

  • full_document (Optional[str]) – The fullDocument to pass as an option to the $changeStream stage. Allowed values: ‘updateLookup’, ‘whenAvailable’, ‘required’. When set to ‘updateLookup’, the change notification for partial updates will include both a delta describing the changes to the document, as well as a copy of the entire document that was changed from some time after the change occurred.

  • full_document_before_change (Optional[str]) – Allowed values: ‘whenAvailable’ and ‘required’. Change events may now result in a ‘fullDocumentBeforeChange’ response field.

  • resume_after (Optional[Mapping[str, Any]]) – A resume token. If provided, the change stream will start returning changes that occur directly after the operation specified in the resume token. A resume token is the _id value of a change document.

  • max_await_time_ms (Optional[int]) – The maximum time in milliseconds for the server to wait for changes before responding to a getMore operation.

  • batch_size (Optional[int]) – The maximum number of documents to return per batch.

  • collation (Optional[_CollationIn]) – The Collation to use for the aggregation.

  • start_at_operation_time (Optional[Timestamp]) – If provided, the resulting change stream will only return changes that occurred at or after the specified Timestamp. Requires MongoDB >= 4.0.

  • session (Optional[client_session.AsyncClientSession]) – a AsyncClientSession.

  • start_after (Optional[Mapping[str, Any]]) – The same as resume_after except that start_after can resume notifications after an invalidate event. This option and resume_after are mutually exclusive.

  • comment (Optional[Any]) – A user-provided comment to attach to this command.

  • show_expanded_events (Optional[bool]) – Include expanded events such as DDL events like dropIndexes.

Returns:

A AsyncClusterChangeStream cursor.

Return type:

AsyncChangeStream[_DocumentType]

Changed in version 4.3: Added show_expanded_events parameter.

Changed in version 4.2: Added full_document_before_change parameter.

Changed in version 4.1: Added comment parameter.

Changed in version 3.9: Added the start_after parameter.

Added in version 3.7.

See also

The MongoDB documentation on changeStreams.

async bulk_write(models, session=None, ordered=True, verbose_results=False, bypass_document_validation=None, comment=None, let=None, write_concern=None)

Send a batch of write operations, potentially across multiple namespaces, to the server.

Requests are passed as a list of write operation instances ( InsertOne, UpdateOne, UpdateMany, ReplaceOne, DeleteOne, or DeleteMany).

>>> async for doc in db.test.find({}):
...     print(doc)
...
{'x': 1, '_id': ObjectId('54f62e60fba5226811f634ef')}
{'x': 1, '_id': ObjectId('54f62e60fba5226811f634f0')}
...
>>> async for doc in db.coll.find({}):
...     print(doc)
...
{'x': 2, '_id': ObjectId('507f1f77bcf86cd799439011')}
...
>>> # DeleteMany, UpdateOne, and UpdateMany are also available.
>>> from pymongo import InsertOne, DeleteOne, ReplaceOne
>>> models = [InsertOne(namespace="db.test", document={'y': 1}),
...           DeleteOne(namespace="db.test", filter={'x': 1}),
...           InsertOne(namespace="db.coll", document={'y': 2}),
...           ReplaceOne(namespace="db.test", filter={'w': 1}, replacement={'z': 1}, upsert=True)]
>>> result = await client.bulk_write(models=models)
>>> result.inserted_count
2
>>> result.deleted_count
1
>>> result.modified_count
0
>>> result.upserted_count
1
>>> async for doc in db.test.find({}):
...     print(doc)
...
{'x': 1, '_id': ObjectId('54f62e60fba5226811f634f0')}
{'y': 1, '_id': ObjectId('54f62ee2fba5226811f634f1')}
{'z': 1, '_id': ObjectId('54f62ee28891e756a6e1abd5')}
...
>>> async for doc in db.coll.find({}):
...     print(doc)
...
{'x': 2, '_id': ObjectId('507f1f77bcf86cd799439011')}
{'y': 2, '_id': ObjectId('507f1f77bcf86cd799439012')}
Parameters:
  • models (Sequence[_WriteOp[_DocumentType]]) – A list of write operation instances.

  • session (Optional[AsyncClientSession]) – (optional) An instance of AsyncClientSession.

  • ordered (bool) – If True (the default), requests will be performed on the server serially, in the order provided. If an error occurs all remaining operations are aborted. If False, requests will be still performed on the server serially, in the order provided, but all operations will be attempted even if any errors occur.

  • verbose_results (bool) – If True, detailed results for each successful operation will be included in the returned ClientBulkWriteResult. Default is False.

  • bypass_document_validation (Optional[bool]) – (optional) If True, allows the write to opt-out of document level validation. Default is False.

  • comment (Optional[Any]) – (optional) A user-provided comment to attach to this command.

  • let (Optional[Mapping]) – (optional) Map of parameter names and values. Values must be constant or closed expressions that do not reference document fields. Parameters can then be accessed as variables in an aggregate expression context (e.g. “$$var”).

  • write_concern (Optional[WriteConcern]) – (optional) The write concern to use for this bulk write.

Returns:

An instance of ClientBulkWriteResult.

Return type:

ClientBulkWriteResult

See also

For more info, see Client Bulk Write Operations.

Note

requires MongoDB server version 8.0+.

Added in version 4.9.

__getitem__(name)

Get a database by name.

Raises InvalidName if an invalid database name is used.

Parameters:

name (str) – the name of the database to get

Return type:

AsyncDatabase[_DocumentType]

__getattr__(name)

Get a database by name.

Raises InvalidName if an invalid database name is used.

Parameters:

name (str) – the name of the database to get

Return type:

AsyncDatabase[_DocumentType]