gpt4 book ai didi

python-3.x - 带有用于 ArangoDB 的 python-arango 驱动程序的 UPSERT

转载 作者:行者123 更新时间:2023-12-01 04:47:02 25 4
gpt4 key购买 nike

我正在使用 python-arango作为ArangoDB的驱动程序,似乎没有UPSERT界面。

我打算用 标记这个python-arango ,但我没有足够的代表来创建新标签。

我正在使用如下所示的功能进行管理,但我想知道是否有更好的方法来做到这一点?

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')))

备注 在我的情况下,我确保所有文档都具有 _key 的值并且在插入之前,因此我可以假设这是成立的。如果其他人想使用它,请相应地修改。

编辑 :删除了 _id 的使用领域,因为这对问题并不重要。

最佳答案

使用要点 upsert是保存来自应用程序的数据库往返,这就是 try/except方法没有那么好。

但是,目前the ArangoDB HTTP-API不提供 upserts,因此 python-arango 无法为您提供 API。

您应该改为使用 AQL query to upsert your document为达到这个:

UPSERT { name: "test" }
INSERT { name: "test" }
UPDATE { } IN users
LET opType = IS_NULL(OLD) ? "insert" : "update"
RETURN { _key: NEW._key, type: opType }

通过 python-arango s db.aql.execute -interface

关于python-3.x - 带有用于 ArangoDB 的 python-arango 驱动程序的 UPSERT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45939062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com