master_slave_connection – Master-slave connection to MongoDB

Master-Slave connection to Mongo.

Performs all writes to Master instance and distributes reads among all slaves. Reads are tried on each slave in turn until the read succeeds or all slaves failed.

class pymongo.master_slave_connection.MasterSlaveConnection(master, slaves=[], document_class=<type 'dict'>, tz_aware=False)

Create a new Master-Slave connection.

The resultant connection should be interacted with using the same mechanisms as a regular MongoClient. The MongoClient instances used to create this MasterSlaveConnection can themselves make use of connection pooling, etc. MongoClient instances used as slaves should be created with the read_preference option set to SECONDARY. Write concerns are inherited from master and can be changed in this instance.

Raises TypeError if master is not an instance of MongoClient or slaves is not a list of at least one MongoClient instances.

Parameters :
  • master: MongoClient instance for the writable Master
  • slaves: list of MongoClient instances for the read-only slaves
  • document_class (optional): default class to use for documents returned from queries on this connection
  • tz_aware (optional): if True, datetime instances returned as values in a document by this MasterSlaveConnection will be timezone aware (otherwise they will be naive)
safe

DEPRECATED: Use the ‘w’ write_concern option instead.

Use getlasterror with every write operation?

New in version 2.0.

get_lasterror_options()

DEPRECATED: Use write_concern instead.

Returns a dict of the getlasterror options set on this instance.

Changed in version 2.4: Deprecated get_lasterror_options.

New in version 2.0.

set_lasterror_options(**kwargs)

DEPRECATED: Use write_concern instead.

Set getlasterror options for this instance.

Valid options include j=<bool>, w=<int/string>, wtimeout=<int>, and fsync=<bool>. Implies safe=True.

Parameters :
  • **kwargs: Options should be passed as keyword

    arguments (e.g. w=2, fsync=True)

Changed in version 2.4: Deprecated set_lasterror_options.

New in version 2.0.

unset_lasterror_options(*options)

DEPRECATED: Use write_concern instead.

Unset getlasterror options for this instance.

If no options are passed unsets all getlasterror options. This does not set safe to False.

Parameters :
  • *options: The list of options to unset.

Changed in version 2.4: Deprecated unset_lasterror_options.

New in version 2.0.

close_cursor(cursor_id, connection_id)

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 :
  • cursor_id: cursor id to close
  • connection_id: id of the MongoClient instance where the cursor was opened
database_names()

Get a list of all database names.

disconnect()

Disconnect from MongoDB.

Disconnecting will call disconnect on all master and slave connections.

See also

Module mongo_client

New in version 1.10.1.

document_class

Default class to use for documents returned on this connection.

drop_database(name_or_database)

Drop a database.

Parameters :
  • name_or_database: the name of a database to drop or the object itself
end_request()

End the current “request”.

See documentation for MongoClient.end_request.

is_mongos

If this MasterSlaveConnection is connected to mongos (always False)

New in version 2.3.

max_bson_size

Return the maximum size BSON object the connected master accepts in bytes. Defaults to 4MB in server < 1.7.4.

New in version 2.6.

max_message_size

Return the maximum message size the connected master accepts in bytes.

New in version 2.6.

max_wire_version

The maxWireVersion reported by the server.

Returns 0 when connected to server versions prior to MongoDB 2.6.

New in version 2.7.

max_write_batch_size

The maxWriteBatchSize reported by the server.

Returns a default value when connected to server versions prior to MongoDB 2.6.

New in version 2.7.

min_wire_version

The minWireVersion reported by the server.

Returns 0 when connected to server versions prior to MongoDB 2.6.

New in version 2.7.

set_cursor_manager(manager_class)

Set the cursor manager for this connection.

Helper to set cursor manager for each individual MongoClient instance that make up this MasterSlaveConnection.

start_request()

Start a “request”.

Start a sequence of operations in which order matters. Note that all operations performed within a request will be sent using the Master connection.

use_greenlets

Whether calling start_request() assigns greenlet-local, rather than thread-local, sockets.

New in version 2.4.2.

Previous topic

errors – Exceptions raised by the pymongo package

Next topic

message – Tools for creating messages to be sent to MongoDB

This Page