pymongo::connection Namespace Reference


class  _Pool
class  Connection


def __add_hosts_and_get_primary
def __check_bson_size
def __check_response_to_last_error
def __cmp__
def __connect
def __find_master
def __getattr__
def __getitem__
def __init__
def __iter__
def __receive_data_on_socket
def __receive_message_on_socket
def __repr__
def __send_and_receive
def __socket
def __try_node
def _cache_index
def _closed
def _parse_uri
def _partition
def _purge_index
def _send_message
def _send_message_with_response
def _str_to_node
def close_cursor
def copy_database
def database_names
def disconnect
def drop_database
def end_request
def from_uri
def get_document_class
def host
def kill_cursors
def max_bson_size
def next
def nodes
def paired
def port
def server_info
def set_cursor_manager
def set_document_class
def slave_okay
def start_request
def tz_aware


int __max_bson_size = 4
float _CONNECT_TIMEOUT = 20.0
string HOST = "localhost"
int PORT = 27017

Function Documentation

def pymongo::connection::__add_hosts_and_get_primary (   self,
) [private]

Definition at line 511 of file

def pymongo::connection::__check_bson_size (   self,
) [private]
Make sure the message doesn't include BSON documents larger
than the connected server will accept.

  - `message`: message to check

Definition at line 686 of file

def pymongo::connection::__check_response_to_last_error (   self,
) [private]
Check a response to a lastError message for errors.

`response` is a byte string representing a response to the message.
If it represents an error response we raise OperationFailure.

Return the response as a document.

Definition at line 656 of file

def pymongo::connection::__cmp__ (   self,

Definition at line 828 of file

def pymongo::connection::__connect (   self  )  [private]
(Re-)connect to Mongo and return a new (connected) socket.

Connect to the master if this is a paired connection.

Definition at line 580 of file

def pymongo::connection::__find_master (   self  )  [private]
Create a new socket and use it to figure out who the master is.

Sets __host and __port so that :attr:`host` and :attr:`port`
will return the address of the master. Also (possibly) updates
any replSet information.

Definition at line 544 of file

def pymongo::connection::__getattr__ (   self,
Get a database by name.

Raises :class:`~pymongo.errors.InvalidName` if an invalid
database name is used.

  - `name`: the name of the database to get

Definition at line 840 of file

def pymongo::connection::__getitem__ (   self,
Get a database by name.

Raises :class:`~pymongo.errors.InvalidName` if an invalid
database name is used.

  - `name`: the name of the database to get

Definition at line 851 of file

def pymongo::connection::__init__ (   self,
  host = None,
  port = None,
  pool_size = None,
  auto_start_request = None,
  timeout = None,
  slave_okay = False,
  network_timeout = None,
  document_class = dict,
  tz_aware = False,
  _connect = True 
Create a new connection to a single MongoDB instance at *host:port*.

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,
:class:`~pymongo.errors.ConnectionFailure` is raised. If
auto-reconnection will be performed,
:class:`~pymongo.errors.AutoReconnect` will be
raised. Application code should handle this exception
(recognizing that the operation failed) and then continue to

Raises :class:`TypeError` if port is not an instance of
``int``. Raises :class:`~pymongo.errors.ConnectionFailure` if
the connection cannot be made.

The `host` parameter can be a full `mongodb URI
<>`_, in addition to
a simple hostname. It can also be a list of hostnames or
URIs. Any port specified in the host string(s) will override
the `port` parameter. If multiple mongodb URIs containing
database or auth information are passed, the last database,
username, and password present will be used.

  - `host` (optional): hostname or IPv4 address of the
    instance to connect to, or a mongodb URI, or a list of
    hostnames / mongodb URIs
  - `port` (optional): port number on which to connect
  - `pool_size` (optional): DEPRECATED
  - `auto_start_request` (optional): DEPRECATED
  - `slave_okay` (optional): is it okay to connect directly to
    and perform queries on a slave instance
  - `timeout` (optional): DEPRECATED
  - `network_timeout` (optional): timeout (in seconds) to use
    for socket operations - default is no timeout
  - `document_class` (optional): default class to use for
    documents returned from queries on this connection
  - `tz_aware` (optional): if ``True``,
    :class:`~datetime.datetime` instances returned as values
    in a document by this :class:`Connection` will be timezone
    aware (otherwise they will be naive)

.. seealso:: :meth:`end_request`
.. versionchanged:: 1.8
   The `host` parameter can now be a full `mongodb URI
   <>`_, in addition
   to a simple hostname. It can also be a list of hostnames or
.. versionadded:: 1.8
   The `tz_aware` parameter.
.. versionadded:: 1.7
   The `document_class` parameter.
.. versionchanged:: 1.4
   DEPRECATED The `pool_size`, `auto_start_request`, and `timeout`
.. versionadded:: 1.1
   The `network_timeout` parameter.

.. mongodoc:: connections

Definition at line 216 of file

def pymongo::connection::__iter__ (   self  ) 

Definition at line 974 of file

def pymongo::connection::__receive_data_on_socket (   self,
) [private]
Lowest level receive operation.

Takes length to receive and repeatedly calls recv until able to
return a buffer of that length, raising ConnectionFailure on error.

Definition at line 738 of file

def pymongo::connection::__receive_message_on_socket (   self,
) [private]
Receive a message in response to `request_id` on `sock`.

Returns the response data with the header removed.

Definition at line 752 of file

def pymongo::connection::__repr__ (   self  ) 

Definition at line 834 of file

def pymongo::connection::__send_and_receive (   self,
) [private]
Send a message on the given socket and return the response data.

Definition at line 766 of file

def pymongo::connection::__socket (   self  )  [private]
Get a socket from the pool.

If it's been > 1 second since the last time we checked out a
socket, we also check to see if the socket has been closed -
this let's us avoid seeing *some*
:class:`~pymongo.errors.AutoReconnect` exceptions on server
hiccups, etc. We only do this if it's been > 1 second since
the last socket checkout, to keep performance reasonable - we
can't avoid those completely anyway.

Definition at line 600 of file

def pymongo::connection::__try_node (   self,
) [private]

Definition at line 518 of file

def pymongo::connection::_cache_index (   self,
) [private]
Add an index to the index cache for ensure_index operations.

Return ``True`` if the index has been newly cached or if the index had
expired and is being re-cached.

Return ``False`` if the index exists and is valid.

Definition at line 389 of file

def pymongo::connection::_closed (   sock  )  [private]
Return True if we know socket has been closed, False otherwise.

Definition at line 141 of file

def pymongo::connection::_parse_uri (   uri,
  default_port = 27017 
) [private]
MongoDB URI parser.

Definition at line 85 of file

def pymongo::connection::_partition (   source,
) [private]
Our own string partitioning method.

Splits `source` on `sub`.

Definition at line 61 of file

def pymongo::connection::_purge_index (   self,
  collection_name = None,
  index_name = None 
) [private]
Purge an index from the index cache.

If `index_name` is None purge an entire collection.

If `collection_name` is None purge an entire database.

Definition at line 418 of file

def pymongo::connection::_send_message (   self,
  with_last_error = False 
) [private]
Say something to Mongo.

Raises ConnectionFailure if the message cannot be sent. Raises
OperationFailure if `with_last_error` is ``True`` and the
response to the getLastError call returns an error. Return the
response from lastError, or ``None`` if `with_last_error`
is ``False``.

  - `message`: message to send
  - `with_last_error`: check getLastError status after sending the

Definition at line 707 of file

def pymongo::connection::_send_message_with_response (   self,
  _must_use_master = False,
) [private]
Send a message to Mongo and return the response.

Sends the given message and returns the response.

  - `message`: (request_id, data) pair making up the message to send

Definition at line 775 of file

def pymongo::connection::_str_to_node (   string,
  default_port = 27017 
) [private]
Convert a string to a node tuple.

"localhost:27017" -> ("localhost", 27017)

Definition at line 72 of file

def pymongo::connection::close_cursor (   self,
Close a single database cursor.

Raises :class:`TypeError` if `cursor_id` is not an instance of
``(int, long)``. What closing the cursor actually means
depends on this connection's cursor manager.

  - `cursor_id`: id of cursor to close

.. seealso:: :meth:`set_cursor_manager` and
   the :mod:`~pymongo.cursor_manager` module

Definition at line 862 of file

def pymongo::connection::copy_database (   self,
  from_host = None,
  username = None,
  password = None 
Copy a database, potentially from another host.

Raises :class:`TypeError` if `from_name` or `to_name` is not
an instance of :class:`basestring`. Raises
:class:`~pymongo.errors.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.

  - `from_name`: the name of the source database
  - `to_name`: the name of the target database
  - `from_host` (optional): host name to copy from
  - `username` (optional): username for source database
  - `password` (optional): password for source database

.. note:: Specifying `username` and `password` requires server
   version **>= 1.3.3+**.

.. versionadded:: 1.5

Definition at line 926 of file

def pymongo::connection::database_names (   self  ) 
Get a list of the names of all databases on the connected server.

Definition at line 898 of file

def pymongo::connection::disconnect (   self  ) 
Disconnect from MongoDB.

Disconnecting will close all underlying sockets in the
connection pool. If the :class:`Connection` is used again it
will be automatically re-opened. Care should be taken to make
sure that :meth:`disconnect` is not called in the middle of a
sequence of operations in which ordering is important. This
could lead to unexpected results.

.. seealso:: :meth:`end_request`
.. versionadded:: 1.3

Definition at line 620 of file

def pymongo::connection::drop_database (   self,
Drop a database.

Raises :class:`TypeError` if `name_or_database` is not an instance of
``(str, unicode, Database)``

  - `name_or_database`: the name of a database to drop, or a
    :class:`~pymongo.database.Database` instance representing the
    database to drop

Definition at line 904 of file

def pymongo::connection::end_request (   self  ) 
Allow this thread's connection to return to the pool.

Calling :meth:`end_request` allows the :class:`~socket.socket`
that has been reserved for this thread to be returned to the
pool. Other threads will then be able to re-use that
:class:`~socket.socket`. If your application uses many
threads, or has long-running threads that infrequently perform
MongoDB operations, then judicious use of this method can lead
to performance gains. Care should be taken, however, to make
sure that :meth:`end_request` is not called in the middle of a
sequence of operations in which ordering is important. This
could lead to unexpected results.

One important case is when a thread is dying permanently. It
is best to call :meth:`end_request` when you know a thread is
finished, as otherwise its :class:`~socket.socket` will not be

Definition at line 807 of file

def pymongo::connection::from_uri (   cls,
  uri = "mongodb://localhost",
DEPRECATED Can pass a mongodb URI directly to Connection() instead.

.. versionchanged:: 1.8
.. versionadded:: 1.5

Definition at line 362 of file

def pymongo::connection::get_document_class (   self  ) 

Definition at line 479 of file

def pymongo::connection::host (   self  ) 
Current connected host.

.. versionchanged:: 1.3
   ``host`` is now a property rather than a method.

Definition at line 444 of file

def pymongo::connection::kill_cursors (   self,
Send a kill cursors message with the given ids.

Raises :class:`TypeError` if `cursor_ids` is not an instance of

  - `cursor_ids`: list of cursor ids to kill

Definition at line 880 of file

def pymongo::connection::max_bson_size (   self  ) 
Return the maximum size BSON object the connected server
accepts in bytes. Defaults to 4MB in server < 1.7.4.

.. versionadded:: 1.10

Definition at line 503 of file

def pymongo::connection::next (   self  ) 

Definition at line 977 of file

def pymongo::connection::nodes (   self  ) 
List of all known nodes.

Includes both nodes specified when the :class:`Connection` was
created, as well as nodes discovered through the replica set
discovery mechanism.

.. versionadded:: 1.8

Definition at line 462 of file

def pymongo::connection::paired (   cls,
  right = None,
DEPRECATED Can pass a list of hostnames to Connection() instead.

.. versionchanged:: 1.8

Definition at line 374 of file

def pymongo::connection::port (   self  ) 
Current connected port.

.. versionchanged:: 1.3
   ``port`` is now a property rather than a method.

Definition at line 453 of file

def pymongo::connection::server_info (   self  ) 
Get information about the MongoDB server we're connected to.

Definition at line 893 of file

def pymongo::connection::set_cursor_manager (   self,
Set this connection's cursor manager.

Raises :class:`TypeError` if `manager_class` is not a subclass of
:class:`~pymongo.cursor_manager.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.

  - `manager_class`: cursor manager to use

Definition at line 637 of file

def pymongo::connection::set_document_class (   self,

Definition at line 482 of file

def pymongo::connection::slave_okay (   self  ) 
Is it okay for this connection to connect directly to a slave?

Definition at line 474 of file

def pymongo::connection::start_request (   self  ) 
DEPRECATED all operations will start a request.

.. versionchanged:: 1.4

Definition at line 798 of file

def pymongo::connection::tz_aware (   self  ) 
Does this connection return timezone-aware datetimes?

See the `tz_aware` parameter to :meth:`Connection`.

.. versionadded:: 1.8

Definition at line 493 of file

Variable Documentation

Definition at line 336 of file

Definition at line 338 of file

Definition at line 344 of file

Definition at line 318 of file

Definition at line 348 of file

Definition at line 341 of file

Definition at line 214 of file

Definition at line 343 of file

Definition at line 303 of file

Definition at line 334 of file

Definition at line 340 of file

Definition at line 319 of file

Definition at line 324 of file

Definition at line 345 of file

Definition at line 58 of file

string pymongo::connection::HOST = "localhost"
Connection to MongoDB.

Definition at line 211 of file

Definition at line 212 of file

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Author(s): Bhaskara Marthi
autogenerated on Fri Jan 11 10:09:06 2013