mongo_replica_set_client – Tools for connecting to a MongoDB replica set

    class pymongo.mongo_replica_set_client.MongoReplicaSetClient(hosts_or_uri, document_class=dict, tz_aware=False, connect=True, \*kwargs*)

    Deprecated alias for MongoClient.

    will be removed in a future version of PyMongo.

    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 : it can connect to a replica set, discover all its members, and monitor the set for stepdowns, elections, and reconfigs.

    The refresh method is removed from MongoReplicaSetClient, 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 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 .

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

    • max_pool_size

      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 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 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 CodecOptions of this instance.

    • read_preference

      Read only access to the read preference of this instance.

      Changed in version 3.0: The 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.

    • database_names(session=None)

      DEPRECATED: Get a list of the names of all databases on the connected server.

      Changed in version 3.7: Deprecated. Use 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 ( in python 3) or Database.

      Parameters:
      • name_or_database: the name of a database to drop, or a instance representing the database to drop
      • session (optional): a ClientSession.

      Changed in version 3.6: Added session parameter.

      The 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_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 with different codec options, read preference, and/or write concern from this MongoClient.

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

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

      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.

    • (manager_class)

      DEPRECATED - Set this client’s cursor manager.

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

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

      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 use based only on the URI in a configuration file.

      Changed in version 3.5: Deprecated, use instead.