4
私はArangoDBのドライバとしてpython-arangoを使用していますが、UPSERT
インターフェイスはないようです。ArangoDBのpython-arangoドライバを使用したUPSERT
は私がのpython-アランゴ、でこれをタグ付けを意図するものではなく、私は新しいタグを作成するのに十分な担当者を持っています。
私は以下の機能のようなもので管理していますが、これを行うより良い方法があるのでしょうか?私の場合、私はそうので、私は、これが成立すると仮定することができ、すべての文書が_key
ため、挿入前に値を持っていることを確認することを
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:
# Add insert_time to document
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:
# Key already exists in collection
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')))
注意。他の誰かがこれを使用したい場合は、それに従って修正してください。
EDIT:これは問題には必須ではないため、_id
フィールドの使用を削除しました。
FWIWでは、IDは常に '{collection}/{_ key}'であるため、 '_key'を設定するだけで済みます。私が知っている唯一の他の方法は、AQLを使用してクエリ内でupsertを使うことです:https://docs.arangodb.com/3.0/AQL/Operations/Upsert.html –
ありがとう、@AndrewGrothe、それは良い点です。私はupsertingの前に私のモデル間のロジックに '_id'値を使用しています。誰かが純粋に私の関数のコピーを実装しようとした場合には、それを指摘しておかなければなりません。 (上記の例では、エラーを記録するためにのみ使用されているので、質問で省略することができたと思います)。 –