Database¶
This module for provide work with MongoDB database.
import pymongo
from yadm.database import Database
from mydocs import Doc
client = pymongo.MongoClient("localhost", 27017)
db = Database(self.client, 'test')
doc = Doc()
db.insert(doc)
doc.arg = 13
db.save(doc)
qs = db.get_queryset(Doc).find({'arg': {'$gt': 10}})
for doc in qs:
print(doc)
-
class
yadm.database.
Database
(client, name)¶ Main object who provide work with database
Parameters: - client (pymongo.Client) – database connection
- name (str) – database name
-
bulk
(document_class, ordered=False, raise_on_errors=True)¶ Return Bulk
Parameters: - document_class (MetaDocument) – class of documents fo bulk
- ordered (bool) – create ordered bulk (default False)
- raise_on_errors (bool) – raise BulkWriteError exception if write errors (default True)
Context manager:
- with db.bulk(Doc) as bulk:
- bulk.insert(doc_1) bulk.insert(doc_2)
-
get_queryset
(document_class)¶ Return queryset for document class
Parameters: document_class – yadm.documents.Document
This create instance of
yadm.queryset.QuerySet
with presetted document’s collection information.
-
insert
(document)¶ Insert document to database
Parameters: document (Document) – document instance for insert to database It’s bind new document to database set
_id
.
-
reload
(document, new_instance=False)¶ Reload document
Parameters: - document (Document) – instance for reload
- new_instance (bool) – if True return new instance of document, else change data in given document (default: False)
-
remove
(document)¶ Remove document from database
Parameters: document (Document) – instance for remove from database