Documents in ArangoDB are JSON objects. These objects can be nested (to any depth) and may contains lists. Each document is unique identified by its document handle.
Usage example:
from arango import create
# connection & collection `test`
c = create(db="test")
c.database.create() # make sure database exists
c.test.create()
# create FROM document
document = c.test.documents.create({
"sample_key": "sample_value"
})
assert document.get("sample_key") == "sample_value"
assert c.test.documents().count != 0
Documents are accessible via collection instance for example connection.collection.sample_collection.documents. Usually this expressions looks lot shorter.
Basically via docuemnts shortcut accessible Docuemnts Proxy - Proxy object which have several shortcuts and produce Resultset object.
Below described basic method within Documents proxy:
Proxy object to handle work with documents within collection instance
Get count of all documents in Collection
Shortcut for new documents creation
Insert bulk of documents using HTTP Interface for bulk imports.
docs = [
{"name": "doc1"},
{"name": "doc2"},
{"name": "doc3"}]
response = c.test.documents.create_bulk(docs)
assert response == {
u'created': 3, u'errors': 0,
u'empty': 0, u'error': False}, "Docs are not created"
Actually, it’s possible to use Headers and values import in this call (and first element in docs have to be attribute names and every element in docs array have to be list). In this case you don’t need to pass key/value pair for every document.
docs = [
["name"],
["doc1"],
["doc2"],
["doc3"]]
response = c.test.documents.create_bulk(docs)
assert response == {
u'created': 3, u'errors': 0,
u'empty': 0, u'error': False}, "Docs are not created"
Delete document shorcut
ref_or_document may be either plai reference or Document instance
Load particular document by id doc_id.
Example:
doc_id = c.test.documents.create({"x": 2}).id
doc = c.test.documents.load(doc_id)
assert doc.body["x"] == 2
Update document
ref_or_document may be either plain reference or Document instance
Document instance methods consist from basic CRUD methods and serveral shortcuts to more convenient work with documents.
Particular instance of Document
Return whole document.
This property setter also should be used if overwriting of whole document is required.
doc_id = c.test.documents.create({"x": 2}).id
doc = c.test.documents.load(doc_id)
assert doc.body["x"] == 2
doc.body = {"x": 1}
doc.save()
assert c.test.documents.load(doc_id).body["x"] == 1
Method to create new document.
Possible arguments: waitForSync
Read more about additional arguments Documents REST Api
This method may raise DocumentAlreadyCreated exception in case document already created.
Return document instance (self) or None
Delete current document.
Return True if success and False if not
This method very similar to dict‘s get method. The difference is that default value should be specified explicitly.
To get specific value for specific key in body use and default (fallback) value 0
c.test.documents().first.get(name="sample_key", default=0)
Method to force save of the document.
kwargs will be passed directly to requests arguments.
Method to update document.
This method is NOT for overwriting document body. In case document is list it will extend current document body. In case it’s dict - update document body with new data.
To overwrite document body use body setter.
In case save argument set to False document will not be updated until save() method will be called.
This method may raise EdgeNotYetCreated exception in case you trying to update edge which is not saved yet.
Exception DocumentIncompatibleDataType will be raised in case body of the document isn’t either dict or list.