MongoDB database¶
Wrapper for MongoDB¶
-
class
orion.core.io.database.mongodb.
MongoDB
(*args, **kwargs)[source]¶ Wrap MongoDB with three primary methods
read
,write
,remove
.- Attributes
- hoststr
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
Disconnect from database.
count
(collection_name[, query])Count the number of documents in a collection which match the query.
drop_index
(collection_name, name)Remove index from the database
ensure_index
(collection_name, keys[, unique])Create given indexes if they do not already exist in database.
Get database arguments needed to create a database instance.
index_information
(collection_name)Return dict of names and sorting order of indexes
Connect to database, unless MongoDB
is_connected
.read
(collection_name[, query, selection])Read a collection and return a value according to the query.
read_and_write
(collection_name, query, data)Read a collection’s document and update the found document.
remove
(collection_name, query)Delete from a collection document[s] which match the query.
write
(collection_name, data[, query])Write new information to a collection.
-
close_connection
()[source]¶ Disconnect from database.
Note
Doesn’t really do anything because MongoDB reopens connection, when a client or client-derived object is accessed.
-
count
(collection_name, query=None)[source]¶ Count the number of documents in a collection which match the query.
See also
orion.core.io.database.AbstractDB.count()
for argument documentation.
-
ensure_index
(collection_name, keys, unique=False)[source]¶ Create given indexes if they do not already exist in database.
See also
orion.core.io.database.AbstractDB.ensure_index()
for argument documentation.
-
classmethod
get_defaults
()[source]¶ Get database arguments needed to create a database instance.
See also
orion.core.io.database.AbstractDB.get_defaults()
for argument documentation.
-
initiate_connection
()[source]¶ Connect to database, unless MongoDB
is_connected
.:raises
orion.core.io.database.DatabaseError
: if connection or authentication fails
-
property
is_connected
¶ True, if practical connection has been achieved.
Note
MongoDB does not do this automatically when creating the client.
-
read
(collection_name, query=None, selection=None)[source]¶ Read a collection and return a value according to the query.
See also
orion.core.io.database.AbstractDB.read()
for argument documentation.
-
read_and_write
(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
orion.core.io.database.AbstractDB.read_and_write()
for argument documentation.
-
remove
(collection_name, query)[source]¶ Delete from a collection document[s] which match the query.
See also
orion.core.io.database.AbstractDB.remove()
for argument documentation.
-
write
(collection_name, data, query=None)[source]¶ Write new information to a collection. Perform insert or update.
See also
orion.core.io.database.AbstractDB.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