Tools for connecting to a MongoDB replica set.
See also
Connecting to a Replica Set for more examples of how to connect to a replica set.
To get a Database instance from a ReplicaSetConnection use either dictionary-style or attribute-style access:
>>> from pymongo import ReplicaSetConnection
>>> c = ReplicaSetConnection('localhost:31017', replicaSet='repl0')
>>> c.test_database
Database(ReplicaSetConnection([u'...', u'...']), u'test_database')
>>> c['test_database']
Database(ReplicaSetConnection([u'...', u'...']), u'test_database')
Create a new connection to a MongoDB replica set.
The resultant connection object has connection-pooling built in. It also performs auto-reconnection when necessary. If an operation fails because of a connection error, ConnectionFailure is raised. If auto-reconnection will be performed, AutoReconnect will be raised. Application code should handle this exception (recognizing that the operation failed) and then continue to execute.
Raises ConnectionFailure if the connection cannot be made.
The hosts_or_uri parameter can be a full mongodb URI, in addition to a string of host:port pairs (e.g. ‘host1:port1,host2:port2’). If hosts_or_uri is None ‘localhost:27017’ will be used.
Parameters : |
Other optional parameters can be passed as keyword arguments:
|
---|
New in version 2.0.1+.
Disconnect from the replica set primary.
Disconnect from all set members.
Get the db_name Database on ReplicaSetConnection c.
Raises InvalidName if an invalid database name is used.
The seed list used to connect to this replica set.
All active and passive (priority 0) replica set members known to this connection. This does not include hidden or slaveDelay members, or arbiters.
The arbiters known to this connection.
The current primary of the replica set.
Returns None if there is no primary.
The secondary members known to this connection.
The read preference for this instance.
See ReadPreference for available options.
New in version 2.0.1+.
The maximum pool size limit set for this connection.
Default class to use for documents returned on this connection.
Does this connection return timezone-aware datetimes?
Use getlasterrer with every write operation?
New in version 2.0.
Returns a dict of the getlasterror options set on this instance.
New in version 2.0.
Set getlasterror options for this instance.
Valid options include j=<bool>, w=<int>, wtimeout=<int>, and fsync=<bool>. Implies safe=True.
Parameters : |
|
---|
New in version 2.0.
Unset getlasterror options for this instance.
If no options are passed unsets all getlasterror options. This does not set safe to False.
Parameters : |
|
---|
New in version 2.0.
Get a list of the names of all databases on the connected server.
Drop a database.
Raises TypeError if name_or_database is not an instance of (str, unicode, Database)
Parameters : |
|
---|
Copy a database, potentially from another host.
Raises TypeError if from_name or to_name is not an instance of basestring. Raises InvalidName if to_name is not a valid database name.
If from_host is None the current host is used as the source. Otherwise the database is copied from from_host.
If the source database requires authentication, username and password must be specified.
Parameters : |
|
---|
Note
Specifying username and password requires server version >= 1.3.3+.
Close a single database cursor.
Raises TypeError if cursor_id is not an instance of (int, long). What closing the cursor actually means depends on this connection’s cursor manager.
Parameters : |
|
---|