MongoDB database¶
orion.core.io.database.mongodb
– Wrapper for MongoDB¶
-
class
orion.core.io.database.mongodb.
MongoDB
(host='localhost', name=None, port=None, username=None, password=None, serverSelectionTimeoutMS=5000)[source]¶ Wrap MongoDB with three primary methods
read
,write
,remove
.Attributes: - host : str
Hostname or MongoDB compliant full credentials+address+database specification.
- Information on MongoDB `connection string
- <https://docs.mongodb.com/manual/reference/connection-string/>`_.
- .. seealso:: :class:`orion.core.io.database.AbstractDB` for more on attributes.
Methods
close_connection
(self)Disconnect from database. count
(self, collection_name[, query])Count the number of documents in a collection which match the query. drop_index
(self, collection_name, name)Remove index from the database ensure_index
(self, collection_name, keys[, …])Create given indexes if they do not already exist in database. index_information
(self, collection_name)Return dict of names and sorting order of indexes initiate_connection
(self)Connect to database, unless MongoDB is_connected
.read
(self, collection_name[, query, selection])Read a collection and return a value according to the query. read_and_write
(self, collection_name, query, …)Read a collection’s document and update the found document. remove
(self, collection_name, query)Delete from a collection document[s] which match the query. write
(self, collection_name, data[, query])Write new information to a collection. -
close_connection
(self)[source]¶ Disconnect from database.
Note
Doesn’t really do anything because MongoDB reopens connection, when a client or client-derived object is accessed.
-
count
(self, collection_name, query=None)[source]¶ Count the number of documents in a collection which match the query.
See also
AbstractDB.count()
for argument documentation.
-
ensure_index
(self, collection_name, keys, unique=False)[source]¶ Create given indexes if they do not already exist in database.
See also
AbstractDB.ensure_index()
for argument documentation.
-
initiate_connection
(self)[source]¶ Connect to database, unless MongoDB
is_connected
.:raises
DatabaseError
: if connection or authentication fails
-
is_connected
¶ True, if practical connection has been achieved.
Note
MongoDB does not do this automatically when creating the client.
-
read
(self, collection_name, query=None, selection=None)[source]¶ Read a collection and return a value according to the query.
See also
AbstractDB.read()
for argument documentation.
-
read_and_write
(self, collection_name, query, data, selection=None)[source]¶ Read a collection’s document and update the found document.
Returns the updated document, or None if nothing found.
See also
AbstractDB.read_and_write()
for argument documentation.
-
orion.core.io.database.mongodb.
mongodb_exception_wrapper
(method)[source]¶ Convert pymongo exceptions to generic exception types defined in src.core.io.database.
Current exception types converted: pymongo.errors.DuplicateKeyError -> DuplicateKeyError pymongo.errors.BulkWriteError[DUPLICATE_KEY_MESSAGES] -> DuplicateKeyError pymongo.errors.ConnectionFailure -> DatabaseError pymongo.errors.OperationFailure(AUTH_FAILED_MESSAGES) -> DatabaseError