MongoSv

class clink.service.MongoSv(conf)[source]

MongoDB interface in application layer

Parameters:conf (MongoConf) –
use_docspec(doc_spec)[source]

Put a document’s specification under management

Parameters:doc_spec (MongoDocSpec) –
use_docspecs(doc_specs)[source]

Put document’s specifications under management

Parameters:doc_specs (list[MongoDocSpec]) –
doc(name)[source]

Return document object

Parameters:name (str) –
Return type:pymongo.collection.Collection
Raises:DocumentNotExist
docs(*args)[source]

Return document objects

Parameters:args (tuple[str]) –
Return type:tuple[pymongo.collection.Collection]
Raises:DocumentNotExist
close()[source]

Close connection to server

clear()[source]

Clear all of data in database

Example

mongo_sv.py
from clink.service import MongoSv, MongoConf, MongoDocSpec
from pymongo import IndexModel, ASCENDING


conf = MongoConf('mongodb://localhost', 'book-db')
mongo_sv = MongoSv(conf)

book_doc_name = 'book'
book_doc_index_1 = IndexModel([('name', ASCENDING)], unique=True)
book_doc_spec = MongoDocSpec(book_doc_name, [book_doc_index_1])

mongo_sv.use_docspec(book_doc_spec)

book_doc = mongo_sv.doc(book_doc_name)
book_doc.insert_one({'name': 'How to Die', 'author': 'Death'})

book = book_doc.find_one({'name': 'How to Die'})
print(book)

Testing

$ python mong_sv.py
{'author': 'Death', '_id': ObjectId('592403e2e7798911eb606720'),
'name': 'How to Die'}