mongo_client – Tools for connecting to MongoDB (original) (raw)

Tools for connecting to MongoDB.

To get a Database instance from aMongoClient use either dictionary-style or attribute-style access:

from pymongo import MongoClient c = MongoClient() c.test_database Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'test_database') c["test-database"] Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'test-database')

class pymongo.mongo_client.MongoClient(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 = MongoClient(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 = MongoClient(uri)

But not when passed as a simple hostname:

client = MongoClient('/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 theInitial DNS Seedlist Discovery specfor more details. Note that the use of SRV URIs implicitly enables TLS support. Pass tls=false in the URI to override.

Note

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

Note

Starting with version 3.0 the MongoClientconstructor no longer blocks while connecting to the server or servers, and it no longer raisesConnectionFailure if they are unavailable, nor ConfigurationErrorif 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 = MongoClient() try: # The ping command is cheap and does not require auth. client.admin.command('ping') except ConnectionFailure: print("Server not available")

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:

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:

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

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

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:

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 theauthSource, authMechanism, and authMechanismPropertiesoptions. Deprecated the socketKeepAlive keyword argument and URI option.socketKeepAlive now defaults to True.

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

The MongoClient constructor no longer blocks while connecting to the server or servers, and it no longer raises ConnectionFailure if they are unavailable, nor ConfigurationErrorif 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, MongoClient accepted a list of standalone MongoDB servers and used the first it could connect to:

MongoClient(['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. Seemongos 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 thecopy_database examples for alternatives.

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

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

client['my_database']

Not:

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.

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 Database on MongoClient 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 recentTopologyDescription.

Returns:

An instance ofTopologyDescription.

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 thereplicaSet option.

Added in version 3.0: MongoClient 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: MongoClient 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 nodescan change over time as MongoClient’s view of the replica set changes. nodes can also be an empty set whenMongoClient 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 CodecOptionsof 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 WriteConcernof this instance.

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

read_concern

Read only access to the ReadConcernof 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 asSessionOptions. See theclient_session module for details and examples.

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

Returns:

An instance of ClientSession.

Parameters:

Return type:

ClientSession

Added in version 3.6.

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

Get a cursor over the databases of the connected server.

Parameters:

Returns:

An instance of CommandCursor.

Return type:

CommandCursor[dict[str, Any]]

Added in version 3.6.

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.

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

Drop a database.

Raises TypeError if name_or_database is not an instance ofstr or Database.

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 = MongoClient(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.Database[_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_concernparameters.

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 Database with the given name and options.

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

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.Database[_DocumentType]

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

server_info(session=None)

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

Parameters:

session (ClientSession | None) – aClientSession.

Return type:

dict[str, Any]

Changed in version 3.6: Added session parameter.

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 $changeStreamstage and returns aClusterChangeStream 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 ClusterChangeStream iterable blocks until the next change document is returned or an error is raised. If thenext() 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 ChangeStream encountered an unrecoverable error or the # resume attempt failed to recreate the cursor. logging.error("...")

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

Parameters:

Returns:

A ClusterChangeStream cursor.

Return type:

ChangeStream[_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.

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, orDeleteMany).

for doc in db.test.find({}): ... print(doc) ... {'x': 1, '_id': ObjectId('54f62e60fba5226811f634ef')} {'x': 1, '_id': ObjectId('54f62e60fba5226811f634f0')} ... 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 = client.bulk_write(models=models) result.inserted_count 2 result.deleted_count 1 result.modified_count 0 result.upserted_count 1 for doc in db.test.find({}): ... print(doc) ... {'x': 1, '_id': ObjectId('54f62e60fba5226811f634f0')} {'y': 1, '_id': ObjectId('54f62ee2fba5226811f634f1')} {'z': 1, '_id': ObjectId('54f62ee28891e756a6e1abd5')} ... for doc in db.coll.find({}): ... print(doc) ... {'x': 2, '_id': ObjectId('507f1f77bcf86cd799439011')} {'y': 2, '_id': ObjectId('507f1f77bcf86cd799439012')}

Parameters:

Returns:

An instance of ClientBulkWriteResult.

Return type:

ClientBulkWriteResult

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:

Database[__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:

Database[__DocumentType_]