mongo_replica_set_client – Tools for connecting to a MongoDB replica set
- class
MongoReplicaSetClient
(_hosts_or_uri, document_class=dict, tz_aware=False, connect=True, **kwargs) - Deprecated alias for .
MongoReplicaSetClient
will be removed in a future version of PyMongo.
Changed in version 3.0: is now the one and onlyclient class for a standalone server, mongos, or replica set.It includes the functionality that had been split intoMongoReplicaSetClient
: itcan connect to a replica set, discover all its members, and monitorthe set for stepdowns, elections, and reconfigs.
The refresh
method is removed from,as are the seeds
and hosts
properties.
On MongoDB >= 3.6, end all server sessions created by this client bysending 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 andthe threads restarted unless auto encryption is enabled. A clientenabled with auto encryption cannot be used again after being closed;any attempt will raise .
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 c.
Raises InvalidName
if an invalid database name is used.
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.
New in version 3.0: MongoClient gained this property in version 3.0 whenMongoReplicaSetClient’s functionality was merged in.
A sequence of (host, port) pairs. Empty if this client is notconnected to a replica set, there are no visible secondaries, or thisclient was created without the replicaSet option.
New in version 3.0: MongoClient gained this property in version 3.0 whenMongoReplicaSetClient’s functionality was merged in.
arbiters
- Arbiters in the replica set.
A sequence of (host, port) pairs. Empty if this client is notconnected to a replica set, there are no arbiters, or this client wascreated without the replicaSet option.
max_pool_size
- The maximum allowable number of concurrent connections to eachconnected server. Requests to a server will block if there aremaxPoolSize outstanding connections to the requested server.Defaults to 100. Cannot be 0.
When a server’s pool has reached max_pool_size, operations for thatserver block waiting for a socket to be returned to the pool. IfwaitQueueTimeoutMS
is set, a blocked operation will raise after a timeout.By default waitQueueTimeoutMS
is not set.
If the client is not connected, this will block until a connection isestablished or raise ServerSelectionTimeoutError if no server isavailable.
- The largest message the connected server accepts in bytes.
If the client is not connected, this will block until a connection isestablished or raise ServerSelectionTimeoutError if no server isavailable.
local_threshold_ms
The local threshold for 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 attribute is now read only.
databasenames
(_session=None)- DEPRECATED: Get a list of the names of all databases on theconnected server.
Parameters:
Changed in version 3.7: Deprecated. Use list_database_names()
instead.
Changed in version 3.6: Added session
parameter.
dropdatabase
(_name_or_database, session=None)- Drop a database.
Raises TypeError
if name_or_database is not an instance ofbasestring
( in python 3) orDatabase
.
Parameters:
- _name_or_database_: the name of a database to drop, or a[<code>Database</code>]($beafcb5a2a9633e8.md#pymongo.database.Database) instance representing thedatabase to drop
- _session_ (optional): a[<code>ClientSession</code>]($9cd063bf36ed4635.md#pymongo.client_session.ClientSession).
Changed in version 3.6: Added session
parameter.
Note
The ofthis client is automatically applied to this operation when usingMongoDB >= 3.4.
Changed in version 3.4: Apply this client’s write concern automatically to this operationwhen connected to MongoDB >= 3.4.
getdatabase
(_name=None, codec_options=None, read_preference=None, write_concern=None, read_concern=None)- Get a with the given name andoptions.
Useful for creating a Database
withdifferent codec options, read preference, and/or write concern fromthis 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
Parameters:
Changed in version 3.5: The name parameter is now optional, defaulting to the databasenamed in the MongoDB connection URI.
- (_cursor_id, address=None)
- DEPRECATED - Send a kill cursors message soon with the given id.
This method may be called from a Cursor
destructor during garbage collection, so it isn’t safe to take alock or do network I/O. Instead, we schedule the cursor to be closedsoon 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 onthe primary or standalone, or a mongos server.
Changed in version 3.7: Deprecated.
Changed in version 3.0: Added address
parameter.
killcursors
(_cursor_ids, address=None)- DEPRECATED - Send a kill cursors message soon with the given ids.
Raises TypeError
if cursor_ids is not an instance oflist
.
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 onthe 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 beclosed on a background thread instead of sending the messageimmediately.
setcursor_manager
(_manager_class)- DEPRECATED - Set this client’s cursor manager.
Raises TypeError
if manager_class is not a subclass of. A cursor managerhandles closing cursors. Different managers can implement differentpolicies in terms of when to actually kill a cursor that hasbeen closed.
Parameters:
Changed in version 3.3: Deprecated, for real this time.
Changed in version 3.0: Undeprecated.
getdefault_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 usebased only on the URI in a configuration file.
Parameters:
- _default_ (optional): the database name to use if no database namewas provided in the URI.
- _codec_options_ (optional): An instance of[<code>CodecOptions</code>]($f5dfe349e82ce60f.md#bson.codec_options.CodecOptions). If <code>None</code> (thedefault) the [<code>codec_options</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.mongo_replica_set_client.MongoReplicaSetClient.codec_options) of this <code>MongoClient</code> isused.
- _read_preference_ (optional): The read preference to use. If<code>None</code> (the default) the [<code>read_preference</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.mongo_replica_set_client.MongoReplicaSetClient.read_preference) of this<code>MongoClient</code> is used. See [<code>read_preferences</code>]($4f651614045067e6.md#module-pymongo.read_preferences)for options.
- _read_concern_ (optional): An instance of[<code>ReadConcern</code>]($ba6aec8e2bd5e77f.md#pymongo.read_concern.ReadConcern). If <code>None</code> (thedefault) the <code>read_concern</code> of this <code>MongoClient</code> isused.
Changed in version 3.8: Undeprecated. Added the default
, codec_options
,read_preference
, write_concern
and parameters.
Changed in version 3.5: Deprecated, use instead.