I am using python-arango as a driver for ArangoDB and there seems to be no interface UPSERT.
I intended to tag this with python-arango , but I don't have enough rep to create new tags.
I control something like the function shown below, but I wonder if there is a better way to do this?
def upsert_document(collection, document, get_existing=False):
"""Upserts given document to a collection. Assumes the _key field is already set in the document dictionary."""
try:
document.update(insert_time=datetime.now().timestamp())
id_rev_key = collection.insert(document)
return document if get_existing else id_rev_key
except db_exception.DocumentInsertError as e:
if e.error_code == 1210:
id_rev_key = collection.update(document)
return collection.get(document.get('_key')) if get_existing else id_rev_key
logging.error('Could not save document {}/{}'.format(collection.name, document.get('_key')))
Note that in my case, I guarantee that all documents have a value for _keyand before insertion, so I can assume that this is done. If someone wants to use this, change accordingly.
EDIT : Removed the use of the field _idas this is not significant to the problem.