Example MongoDB storage interface

With the storage interface we can store documents into a database.

[1]:
import qilib.utils.storage

try:
    storage = qilib.utils.storage.StorageMongoDb('testdb', connection_timeout=.5)
except qilib.utils.storage.mongo.ConnectionTimeoutError:
    # when no MongoDB is running, we use StorageMemory as an example
    storage = qilib.utils.storage.StorageMemory('testdb')

storage.save_data({'string': 'hello', 'float': 1.7}, ['stored_document'])
storage.save_data('hello alice', ['list', 'subdocument1'])
storage.save_data('hello bob', ['list', 'subdocument2'])

The documents are stored in a tree-linke structure. We can list stored documents using list_data_subtags.

[2]:
print(storage.list_data_subtags([]))
print(storage.list_data_subtags(['list']))
['stored_document', 'list']
['subdocument1', 'subdocument2']

A GUI for the database is MongoDB Compass. To search for documents in the database, enter a query in the FILTER field. For example to search for all documents with tag containing the text settings use the query {tag: /settings/}.

mdc.PNG

[ ]:

[ ]: