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('localhost', 27017), u'test_database')
>>> c['test-database']
Database(MongoClient('localhost', 27017), 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,
ConnectionFailureis 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 escaped following RFC 2396.
Warning
When using PyMongo in a multiprocessing context, please read Using PyMongo with Multiprocessing first.
Parameters: - host (optional): hostname or IP address of the instance to connect to, or a mongodb URI, or a list of hostnames / mongodb URIs. If host is an IPv6 literal it must be enclosed in ‘[‘ and ‘]’ characters following the RFC2732 URL syntax (e.g. ‘[::1]’ for localhost)
- port (optional): port number on which to connect
- document_class (optional): default class to use for documents returned from queries on this client
- tz_aware (optional): if
True,datetimeinstances returned as values in a document by thisMongoClientwill be timezone aware (otherwise they will be naive) - connect (optional): if
True(the default), immediately begin connecting to MongoDB in the background. Otherwise connect on the first operation.
Other optional parameters can be passed as keyword arguments:- maxPoolSize (optional): The maximum number of connections that the pool will open simultaneously. If this is set, operations will block if there are maxPoolSize outstanding connections from the pool. Defaults to 100. Cannot be 0.
- 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. 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. Defaults to
20000(20 seconds). - 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). - waitQueueMultiple: (integer or None) Multiplied by maxPoolSize
to give the number of threads allowed to wait for a socket at one
time. Defaults to
None(no limit). - socketKeepAlive: (boolean) Whether to send periodic keep-alive
packets on connected sockets. Defaults to
False(do not send keep-alive packets). - event_listeners: a list or tuple of event listeners. See
monitoringfor details.
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.
- wtimeout: (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.
- j: If
Trueblock until write operations have been committed to the journal. Cannot be used in combination with fsync. Prior to MongoDB 2.6 this option was ignored if the server was running without journaling. Starting with MongoDB 2.6 write operations will fail with an exception if this option is used when the server is running without journaling. - fsync: If
Trueand 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: The read preference for this client.
See
ReadPreferencefor all available read preference options. Defaults toPRIMARY.
SSL configuration:- ssl: If
True, create the connection to the server using SSL. Defaults toFalse. - ssl_keyfile: The private keyfile used to identify the local
connection against mongod. If included with the
certfilethen only thessl_certfileis needed. Impliesssl=True. Defaults toNone. - ssl_certfile: The certificate file used to identify the local
connection against mongod. Implies
ssl=True. Defaults toNone. - ssl_cert_reqs: Specifies whether a certificate is required from
the other side of the connection, and whether it will be validated
if provided. It must be one of the three values
ssl.CERT_NONE(certificates ignored),ssl.CERT_OPTIONAL(not required, but validated if provided), orssl.CERT_REQUIRED(required and validated). If the value of this parameter is notssl.CERT_NONEand a value is not provided forssl_ca_certsPyMongo will attempt to load system provided CA certificates. If the python version in use does not support loading system CA certificates then thessl_ca_certsparameter must point to a file of CA certificates. Impliesssl=True. Defaults tossl.CERT_REQUIREDif not provided andssl=True. - ssl_ca_certs: The ca_certs file contains a set of concatenated
“certification authority” certificates, which are used to validate
certificates passed from the other end of the connection.
Implies
ssl=True. Defaults toNone. - ssl_match_hostname: If
True(the default), and ssl_cert_reqs is notssl.CERT_NONE, enables hostname verification using thematch_hostname()function from python’ssslmodule. Think very carefully before setting this toFalseas that could make your application vulnerable to man-in-the-middle attacks.
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
majoritywill only return data that has been written to a majority of nodes. If the level is left unspecified, the server default will be used.
Changed in version 3.0:
MongoClientis 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
MongoClientconstructor no longer blocks while connecting to the server or servers, and it no longer raisesConnectionFailureif they are unavailable, norConfigurationErrorif the user’s credentials are wrong. Instead, the constructor returns immediately and launches the connection process on background threads.Therefore the
alivemethod 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,
MongoClientaccepted 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
connectoption is added.The
start_request,in_request, andend_requestmethods are removed, as well as theauto_start_requestoption.The
copy_databasemethod is removed, see the copy_database examples for alternatives.The
MongoClient.disconnect()method is removed; it was a synonym forclose().MongoClientno longer returns an instance ofDatabasefor attribute names with leading underscores. You must use dict-style lookups instead:client['__my_database__']
Not:
client.__my_database__
-
close()¶ Disconnect from MongoDB.
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.
-
c[db_name] || c.db_name Get the db_name
DatabaseonMongoClientc.Raises
InvalidNameif an invalid database name is used.
-
address¶ (host, port) of the current standalone, primary, or mongos, or None.
Accessing
addressraisesInvalidOperationif the client is load-balancing among mongoses, since there is no single address. Usenodesinstead.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.
-
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 number of sockets the pool will open concurrently.
When the pool has reached max_pool_size, operations block waiting for a socket to be returned to the pool. If
waitQueueTimeoutMSis set, a blocked operation will raiseConnectionFailureafter a timeout. By defaultwaitQueueTimeoutMSis not set.
-
nodes¶ Set of all currently connected servers.
Warning
When connected to a replica set the value of
nodescan change over time asMongoClient‘s view of the replica set changes.nodescan also be an empty set whenMongoClientis 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.
-
local_threshold_ms¶ The local threshold for this instance.
-
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_preferenceattribute is now read only.
-
write_concern¶ Read only access to the
WriteConcernof this instance.Changed in version 3.0: The
write_concernattribute is now read only.
-
is_locked¶ Is this server locked? While locked, all write operations are blocked, although read operations may still be allowed. Use
unlock()to unlock.
-
database_names()¶ Get a list of the names of all databases on the connected server.
-
drop_database(name_or_database)¶ Drop a database.
Raises
TypeErrorif name_or_database is not an instance ofbasestring(strin python 3) orDatabase.Parameters: - name_or_database: the name of a database to drop, or a
Databaseinstance representing the database to drop
- name_or_database: the name of a database to drop, or a
-
get_default_database()¶ 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'
Useful in scripts where you want to choose which database to use based only on the URI in a configuration file.
-
get_database(name, codec_options=None, read_preference=None, write_concern=None, read_concern=None)¶ Get a
Databasewith the given name and options.Useful for creating a
Databasewith different codec options, read preference, and/or write concern from thisMongoClient.>>> 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: The name of the database - a string.
- codec_options (optional): An instance of
CodecOptions. IfNone(the default) thecodec_optionsof thisMongoClientis used. - read_preference (optional): The read preference to use. If
None(the default) theread_preferenceof thisMongoClientis used. Seeread_preferencesfor options. - write_concern (optional): An instance of
WriteConcern. IfNone(the default) thewrite_concernof thisMongoClientis used. - read_concern (optional): An instance of
ReadConcern. IfNone(the default) theread_concernof thisMongoClientis used.
-
server_info()¶ Get information about the MongoDB server we’re connected to.
-
close_cursor(cursor_id, address=None)¶ Close a single database cursor.
Raises
TypeErrorif cursor_id is not an instance of(int, long). What closing the cursor actually means depends on this client’s cursor manager.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.0: Added
addressparameter.
-
kill_cursors(cursor_ids, address=None)¶ Send a kill cursors message soon with the given ids.
Raises
TypeErrorif cursor_ids is not an instance oflist.This method may be called from a
Cursordestructor 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_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.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)¶ Set this client’s cursor manager.
Raises
TypeErrorif manager_class is not a subclass ofCursorManager. 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.0: Undeprecated.
-
fsync(**kwargs)¶ Flush all pending writes to datafiles.
Parameters: 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.
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()¶ Unlock a previously locked server.