mongo_replica_set_client – Tools for connecting to a MongoDB replica set

Deprecated. See High Availability and PyMongo.

  • class pymongo.mongoreplica_set_client.MongoReplicaSetClient(_hosts_or_uri, document_class=dict, tz_aware=False, connect=True, **kwargs)
  • Deprecated alias for MongoClient.

MongoReplicaSetClientwill be removed in a future version of PyMongo.

Changed in version 3.0: MongoClient 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 fromMongoReplicaSetClient,as are the seeds and hosts properties.

  • close()
  • Cleanup client resources and disconnect from MongoDB.

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 InvalidOperation.

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

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.

  • secondaries
  • The secondary members known to this client.

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 raiseConnectionFailure after a timeout.By default waitQueueTimeoutMS is not set.

  • 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 isestablished or raise ServerSelectionTimeoutError if no server isavailable.

  • max_message_size
  • 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.

  • 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.

  • databasenames(_session=None)
  • DEPRECATED: Get a list of the names of all databases on theconnected server.

Parameters:

  1. - _session_ (optional): a[<code>ClientSession</code>]($9cd063bf36ed4635.md#pymongo.client_session.ClientSession).

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 (str in python 3) orDatabase.

Parameters:

  1. - _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
  2. - _session_ (optional): a[<code>ClientSession</code>]($9cd063bf36ed4635.md#pymongo.client_session.ClientSession).

Changed in version 3.6: Added session parameter.

Note

The write_concern 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 Database with the given name andoptions.

Useful for creating a Database withdifferent codec options, read preference, and/or write concern fromthis MongoClient.

  1. >>> client.read_preference
  2. Primary()
  3. >>> db1 = client.test
  4. >>> db1.read_preference
  5. Primary()
  6. >>> from pymongo import ReadPreference
  7. >>> db2 = client.get_database(
  8. ... 'test', read_preference=ReadPreference.SECONDARY)
  9. >>> db2.read_preference
  10. Secondary(tag_sets=None)

Parameters:

  1. - _name_ (optional): The name of the database - a string. If <code>None</code>(the default) the database named in the MongoDB connection URI isreturned.
  2. - _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.
  3. - _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.
  4. - _write_concern_ (optional): An instance of[<code>WriteConcern</code>]($1c33c29d27c9df5c.md#pymongo.write_concern.WriteConcern). If <code>None</code> (thedefault) the [<code>write_concern</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.mongo_replica_set_client.MongoReplicaSetClient.write_concern) of this <code>MongoClient</code> isused.
  5. - _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.5: The name parameter is now optional, defaulting to the databasenamed in the MongoDB connection URI.

  • closecursor(_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 meansdepends on this client’s cursor manager.

This method may be called from a Cursordestructor 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:

  1. - _cursor_id_: id of cursor to close
  2. - _address_ (optional): (host, port) pair of the cursors 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:

  1. - _cursor_ids_: list of cursor ids to kill
  2. - _address_ (optional): (host, port) pair of the cursors 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 ofCursorManager. A cursor managerhandles closing cursors. Different managers can implement differentpolicies in terms of when to actually kill a cursor that hasbeen closed.

Parameters:

  1. - _manager_class_: cursor manager to use

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.
  1. >>> uri = 'mongodb://host/my_database'
  2. >>> client = MongoClient(uri)
  3. >>> db = client.get_default_database()
  4. >>> assert db.name == 'my_database'
  5. >>> db = client.get_database()
  6. >>> 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:

  1. - _default_ (optional): the database name to use if no database namewas provided in the URI.
  2. - _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.
  3. - _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.
  4. - _write_concern_ (optional): An instance of[<code>WriteConcern</code>]($1c33c29d27c9df5c.md#pymongo.write_concern.WriteConcern). If <code>None</code> (thedefault) the [<code>write_concern</code>](https://api.mongodb.com/python/current/api/pymongo/#pymongo.mongo_replica_set_client.MongoReplicaSetClient.write_concern) of this <code>MongoClient</code> isused.
  5. - _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 read_concernparameters.

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