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 Database instance from a MongoClient 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), u'test_database')
>>> c['test-database']
Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), u'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.
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 or URIs. Any port specified in the host string(s) will override the port parameter. If multiple mongodb URIs containing database or auth information are passed, the last database, username, and password present will be used. For username and passwords reserved characters like ‘:’, ‘/’, ‘+’ and ‘@’ must be percent encoded following RFC 2396:
try:
# Python 3.x
from urllib.parse import quote_plus
except ImportError:
# Python 2.x
from urllib 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 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
MongoClient creation will block waiting for answers from DNS when mongodb+srv:// URIs are used.
Note
Starting with version 3.0 the MongoClient 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 = MongoClient()
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: |
Other optional parameters can be passed as keyword arguments:
Write Concern options: (Only set if passed. No default values.)
Replica set keyword arguments for connecting with a replica set - either directly or via a mongos:
Read Preference:
See also Authentication:
See also TLS/SSL configuration:
Read Concern options: (If not set explicitly, this will use the server default)
Client side encryption options: (If not set explicitly, client side encryption will not be enabled.)
Versioned API options: (If not set explicitly, Versioned API will not be enabled.)
|
---|
See also
The MongoDB documentation on connections.
Changed in version 3.12: Added the server_api
keyword argument. The following keyword arguments were deprecated:
ssl_certfile
andssl_keyfile
were deprecated in favor oftlsCertificateKeyFile
.
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 ofwTimeoutMS
.j
was deprecated in favor ofjournal
.ssl_cert_reqs
was deprecated in favor oftlsAllowInvalidCertificates
.ssl_match_hostname
was deprecated in favor oftlsAllowInvalidHostnames
.ssl_ca_certs
was deprecated in favor oftlsCAFile
.ssl_certfile
was deprecated in favor oftlsCertificateKeyFile
.ssl_crlfile
was deprecated in favor oftlsCRLFile
.ssl_pem_passphrase
was deprecated in favor oftlsCertificateKeyFilePassword
.
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: 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 into MongoReplicaSetClient
: 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 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, 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. 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 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:
client.__my_database__
close
()Cleanup client resources and disconnect from MongoDB.
On MongoDB >= 3.6, 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. If this instance is used again it will be automatically re-opened and the threads restarted unless auto encryption is enabled. A client enabled with auto encryption cannot be used again after being closed; any attempt will raise InvalidOperation.
Changed in version 3.6: End all server sessions created by this client.
c[db_name] || c.db_name
Get the db_name Database on MongoClient c.
Raises InvalidName if an invalid database name is used.
event_listeners
The event listeners registered for this client.
See monitoring for details.
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. New in version 3.12.
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.
New 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.New in version 3.0: MongoClient gained this property in version 3.0 when MongoReplicaSetClient’s functionality was merged in.
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.
New in version 3.0: MongoClient gained this property in version 3.0 when MongoReplicaSetClient’s functionality was merged in.
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..
max_pool_size
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. Cannot be 0.
When a server’s pool has reached max_pool_size, operations for that server block waiting for a socket to be returned to the pool. If
waitQueueTimeoutMS
is set, a blocked operation will raise ConnectionFailure after a timeout. By defaultwaitQueueTimeoutMS
is not set.min_pool_size
The minimum required number of concurrent connections that the pool will maintain to each connected server. Default is 0.
max_idle_time_ms
The maximum number of milliseconds that a connection can remain idle in the pool before being removed and replaced. Defaults to None (no limit).
nodes
Set of all currently connected servers.
Warning
When connected to a replica set the value of nodes can change over time as MongoClient’s view of the replica set changes. nodes can also be an empty set when MongoClient is first instantiated and hasn’t yet connected to any servers, or a network partition causes it to lose connection to all servers.
max_bson_size
The largest BSON object the connected server accepts in bytes.
If the client is not connected, this will block until a connection is established or raise ServerSelectionTimeoutError if no server is available.
max_message_size
The largest message the connected server accepts in bytes.
If the client is not connected, this will block until a connection is established or raise ServerSelectionTimeoutError if no server is available.
max_write_batch_size
The maxWriteBatchSize reported by the server.
If the client is not connected, this will block until a connection is established or raise ServerSelectionTimeoutError if no server is available.
Returns a default value when connected to server versions prior to MongoDB 2.6.
local_threshold_ms
The local threshold for this instance.
server_selection_timeout
The server selection timeout for this instance in seconds.
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.
New in version 3.2.
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.
Requires MongoDB 3.6. It is an error to call start_session() if this client has been authenticated to multiple databases using the deprecated method authenticate().
A ClientSession may only be used with the MongoClient that started it.
ClientSession
instances are not thread-safe or fork-safe. They can only be used by one thread or process at a time. A singleClientSession
cannot be used to run multiple operations concurrently.Returns: An instance of ClientSession. New in version 3.6.
list_databases
(session=None, \*kwargs*)Get a cursor over the databases of the connected server.
Parameters: - session (optional): a ClientSession.
- **kwargs (optional): Optional parameters of the listDatabases command can be passed as keyword arguments to this method. The supported options differ by server version.
Returns: An instance of CommandCursor.
New in version 3.6.
list_database_names
(session=None)Get a list of the names of all databases on the connected server.
Parameters: - session (optional): a ClientSession.
New in version 3.6.
database_names
(session=None)DEPRECATED: Get a list of the names of all databases on the connected server.
Parameters: - session (optional): a ClientSession.
Changed in version 3.7: Deprecated. Use list_database_names() instead.
Changed in version 3.6: Added
session
parameter.drop_database
(name_or_database, session=None)Drop a database.
Raises TypeError if name_or_database is not an instance of
basestring
(str in python 3) or Database.Parameters: - name_or_database: the name of a database to drop, or a Database instance representing the database to drop
- session (optional): a ClientSession.
Changed in version 3.6: Added
session
parameter.Note
The write_concern of this client is automatically applied to this operation when using MongoDB >= 3.4.
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: - default (optional): the database name to use if no database name was provided in the URI.
- codec_options (optional): An instance of CodecOptions. If
None
(the default) the codec_options of this MongoClient is used. - read_preference (optional): The read preference to use. If
None
(the default) the read_preference of this MongoClient is used. See read_preferences for options. - write_concern (optional): An instance of WriteConcern. If
None
(the default) the write_concern of this MongoClient is used. - read_concern (optional): An instance of ReadConcern. If
None
(the default) the read_concern of this MongoClient is used.
Changed in version 3.8: Undeprecated. Added the
default
,codec_options
,read_preference
,write_concern
andread_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 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: - name (optional): The name of the database - a string. If
None
(the default) the database named in the MongoDB connection URI is returned. - codec_options (optional): An instance of CodecOptions. If
None
(the default) the codec_options of this MongoClient is used. - read_preference (optional): The read preference to use. If
None
(the default) the read_preference of this MongoClient is used. See read_preferences for options. - write_concern (optional): An instance of WriteConcern. If
None
(the default) the write_concern of this MongoClient is used. - read_concern (optional): An instance of ReadConcern. If
None
(the default) the read_concern of this MongoClient is used.
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 (optional): a ClientSession.
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)Watch changes on this cluster.
Performs an aggregation with an implicit initial
$changeStream
stage and returns a ClusterChangeStream 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 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 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 the change streams specification.
Parameters: - pipeline (optional): 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): The fullDocument to pass as an option to the
$changeStream
stage. Allowed values: ‘updateLookup’. 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. - resume_after (optional): 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): The maximum time in milliseconds for the server to wait for changes before responding to a getMore operation.
- batch_size (optional): The maximum number of documents to return per batch.
- collation (optional): The Collation to use for the aggregation.
- start_at_operation_time (optional): If provided, the resulting change stream will only return changes that occurred at or after the specified Timestamp. Requires MongoDB >= 4.0.
- session (optional): a ClientSession.
- start_after (optional): The same as resume_after except that start_after can resume notifications after an invalidate event. This option and resume_after are mutually exclusive.
Returns: A ClusterChangeStream cursor.
Changed in version 3.9: Added the
start_after
parameter.New in version 3.7.
See also
The MongoDB documentation on changeStreams.
close_cursor
(cursor_id, address=None)DEPRECATED - Send a kill cursors message soon with the given id.
Raises TypeError if cursor_id is not an instance of
(int, long)
. What closing the cursor actually means depends on this client’s cursor manager.This method may be called from a Cursor destructor during garbage collection, so it isn’t safe to take a lock or do network I/O. Instead, we schedule the cursor to be closed soon on a background thread.
Parameters: - cursor_id: id of cursor to close
- address (optional): (host, port) pair of the cursor’s server. If it is not provided, the client attempts to close the cursor on the primary or standalone, or a mongos server.
Changed in version 3.7: Deprecated.
Changed in version 3.0: Added
address
parameter.kill_cursors
(cursor_ids, address=None)DEPRECATED - Send a kill cursors message soon with the given ids.
Raises TypeError if cursor_ids is not an instance of
list
.Parameters: - cursor_ids: list of cursor ids to kill
- address (optional): (host, port) pair of the cursor’s server. If it is not provided, the client attempts to close the cursor on the primary or standalone, or a mongos server.
Changed in version 3.3: Deprecated.
Changed in version 3.0: Now accepts an address argument. Schedules the cursors to be closed on a background thread instead of sending the message immediately.
set_cursor_manager
(manager_class)DEPRECATED - Set this client’s cursor manager.
Raises TypeError if manager_class is not a subclass of CursorManager. A cursor manager handles closing cursors. Different managers can implement different policies in terms of when to actually kill a cursor that has been closed.
Parameters: - manager_class: cursor manager to use
Changed in version 3.3: Deprecated, for real this time.
Changed in version 3.0: Undeprecated.
is_locked
DEPRECATED: Is this server locked? While locked, all write operations are blocked, although read operations may still be allowed. Use unlock() to unlock.
Deprecated. Users of MongoDB version 3.2 or newer can run the currentOp command directly with command():
is_locked = client.admin.command('currentOp').get('fsyncLock')
Users of MongoDB version 2.6 and 3.0 can query the “inprog” virtual collection:
is_locked = client.admin["$cmd.sys.inprog"].find_one().get('fsyncLock')
Changed in version 3.11: Deprecated.
fsync
(\*kwargs*)DEPRECATED: Flush all pending writes to datafiles.
Optional parameters can be passed as keyword arguments:
- lock: If True lock the server to disallow writes.
- async: If True don’t block while synchronizing.
- session (optional): a ClientSession.
Note
Starting with Python 3.7 async is a reserved keyword. The async option to the fsync command can be passed using a dictionary instead:
options = {'async': True}
client.fsync(**options)
Deprecated. Run the fsync command directly with command() instead. For example:
client.admin.command('fsync', lock=True)
Changed in version 3.11: Deprecated.
Changed in version 3.6: Added
session
parameter.Warning
async and lock can not be used together.
Warning
MongoDB does not support the async option on Windows and will raise an exception on that platform.
unlock
(session=None)DEPRECATED: Unlock a previously locked server.
Parameters: - session (optional): a ClientSession.
Deprecated. Users of MongoDB version 3.2 or newer can run the fsyncUnlock command directly with command():
client.admin.command('fsyncUnlock')
Users of MongoDB version 2.6 and 3.0 can query the “unlock” virtual collection:
client.admin["$cmd.sys.unlock"].find_one()
Changed in version 3.11: Deprecated.
Changed in version 3.6: Added
session
parameter.