Tools for connecting to MongoDB.
See also
Module master_slave_connection for connecting to master-slave clusters, and Connecting to a Replica Set for an example of how to connect to a replica set.
To get a Database instance from a Connection use either dictionary-style or attribute-style access:
>>> from pymongo import Connection
>>> c = Connection()
>>> c.test_database
Database(Connection('localhost', 27017), u'test_database')
>>> c['test-database']
Database(Connection('localhost', 27017), u'test-database')
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, 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 TypeError if port is not an instance of int. Raises 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.
Parameters : |
Other optional parameters can be passed as keyword arguments:
|
---|
See also
Changed in version 2.0.1+: Support w = integer or string. Added ssl option. DEPRECATED slave_okay/slaveOk.
Changed in version 2.0: slave_okay is a pure keyword argument. Added support for safe, and getlasterror options as keyword arguments.
Changed in version 1.11: Added max_pool_size. Completely removed previously deprecated pool_size, auto_start_request and timeout parameters.
Changed in version 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 URIs.
New in version 1.8: The tz_aware parameter.
New in version 1.7: The document_class parameter.
New in version 1.1: The network_timeout parameter.
See general MongoDB documentation
DEPRECATED Can pass a mongodb URI directly to Connection() instead.
Changed in version 1.8: DEPRECATED
New in version 1.5.
DEPRECATED Can pass a list of hostnames to Connection() instead.
Changed in version 1.8: DEPRECATED
Disconnect from MongoDB.
Disconnecting will close all underlying sockets in the connection pool. If the Connection is used again it will be automatically re-opened. Care should be taken to make sure that disconnect() is not called in the middle of a sequence of operations in which ordering is important. This could lead to unexpected results.
See also
New in version 1.3.
Alias for disconnect()
Disconnecting will close all underlying sockets in the connection pool. If the Connection is used again it will be automatically re-opened. Care should be taken to make sure that disconnect() is not called in the middle of a sequence of operations in which ordering is important. This could lead to unexpected results.
See also
New in version 2.0.1+.
Get the db_name Database on Connection c.
Raises InvalidName if an invalid database name is used.
Current connected host.
Changed in version 1.3: host is now a property rather than a method.
Current connected port.
Changed in version 1.3: port is now a property rather than a method.
List of all known nodes.
Includes both nodes specified when the Connection was created, as well as nodes discovered through the replica set discovery mechanism.
New in version 1.8.
The maximum pool size limit set for this connection.
New in version 1.11.
Default class to use for documents returned on this connection.
New in version 1.7.
Does this connection return timezone-aware datetimes?
See the tz_aware parameter to Connection().
New in version 1.8.
The read preference for this instance.
See ReadPreference for available options.
New in version 2.0.1+.
DEPRECATED. Use read_preference instead.
Changed in version 2.0.1+.
New in version 2.0.
Use getlasterrer with every write operation?
New in version 2.0.
Is this server locked? While locked, all write operations are blocked, although read operations may still be allowed. Use unlock() to unlock.
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+.
New in version 1.5.
Get information about the MongoDB server we’re connected to.
DEPRECATED all operations will start a request.
Changed in version 1.4: DEPRECATED
Allow this thread’s connection to return to the pool.
Calling end_request() allows the socket that has been reserved for this thread to be returned to the pool. Other threads will then be able to re-use that 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 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 end_request() when you know a thread is finished, as otherwise its socket will not be reclaimed.
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 : |
|
---|
See also
set_cursor_manager() and the cursor_manager module
Send a kill cursors message with the given ids.
Raises TypeError if cursor_ids is not an instance of list.
Parameters : |
|
---|
Set this connection’s cursor manager.
Raises TypeError 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 : |
|
---|
Flush all pending writes to datafiles.
Parameters : | Optional parameters can be passed as keyword arguments:
Warning async and lock can not be used together. Warning MongoDB does not support the async option on Windows and will raise an exception on that platform. |
---|
New in version 2.0.
Unlock a previously locked server.
New in version 2.0.