Documents¶
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 document
document = c.test.documents.create({
"sample_key": "sample_value"
})
assert document.get("sample_key") == "sample_value"
assert c.test.documents().count != 0
Documents for Collection instance¶
Documents are accessible via collection instance for example connection.collection.sample_collection.documents
. Usually this expressions looks lot shorter.
Basically via documents shortcut accessible Docuemnts Proxy - Proxy object which have several shortcuts and produce Resultset
object.
Below described basic method within Documents
proxy:
-
class
arango.document.
Documents
(collection=None)¶ Proxy object to handle work with documents within collection instance
-
count
¶ Get count of all documents in Collection
-
create
(*args, **kwargs)¶ Shortcut for new documents creation
-
create_bulk
(docs, batch=100)¶ 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 indocs
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
(ref_or_document)¶ Delete document shorcut
ref_or_document
may be either plai reference or Document instance
-
load
(doc_id)¶ 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
(ref_or_document, *args, **kwargs)¶ Update document
ref_or_document
may be either plain reference or Document instance
-
Document¶
Document instance methods consist of basic CRUD methods and several convience functions that act as shortcuts for more convenient work with documents.
-
class
arango.document.
Document
(collection=None, id=None, rev=None, resource_url=None, connection=None)¶ Particular instance of Document
-
body
¶ 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
-
create
(body, createCollection=False, **kwargs)¶ 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
) orNone
-
delete
()¶ Delete current document.
Return
True
if success andFalse
if not
-
get
(name=None, default=None)¶ This method very similar to
dict
‘sget
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)
-
save
(**kwargs)¶ Method to force save of the document.
kwargs
will be passed directly torequests
arguments.
-
update
(newData, save=True, **kwargs)¶ 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’sdict
- update document body with new data.To overwrite document body use
body
setter.In case
save
argument set toFalse
document will not be updated untilsave()
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
orlist
.
-