[英]Arangodb insert multiple rows and using UPSERT
how do i insert multiple rows in arangodb with UPSERT? 如何使用UPSERT在arangodb中插入多行? Collection contains a unique index which prevent insert duplicate documents.
集合包含唯一索引,可以防止插入重复的文档。 multiple insert work fine without unique index, but how can i handle update/replace in multiple insert with unique index?
没有唯一索引的情况下,多次插入工作正常,但是如何处理具有唯一索引的多次插入中的更新/替换?
like this: 像这样:
INSERT [{doc1},{doc2},{doc3}]
IN collection
UPDATE {} // when duplicate per document
Update 1 更新1
SQL look like this: SQL看起来像这样:
INSERT INTO table(name, value)
VALUES('a', '1'), ('b', 2), ('c', 3)
ON DUPLICATE KEY UPDATE name=`value`
thanks. 谢谢。
ArangoDB supports UPSERT operation: https://docs.arangodb.com/3.3/AQL/Operations/Upsert.html ArangoDB支持UPSERT操作: https ://docs.arangodb.com/3.3/AQL/Operations/Upsert.html
From ArangoDB documentation: 从ArangoDB文档中:
When using the UPDATE variant of the upsert operation, the found document will be partially updated, meaning only the attributes specified in updateExpression will be updated or added.
当使用upsert操作的UPDATE变体时,找到的文档将被部分更新,这意味着将仅更新或添加在updateExpression中指定的属性。 When using the REPLACE variant of upsert, existing documents will be replaced with the contexts of updateExpression.
当使用upsert的REPLACE变体时,现有文档将被updateExpression的上下文替换。
You can use UPSERT to update/replace/insert multiple records as following: 您可以使用UPSERT更新/替换/插入多个记录,如下所示:
Let's insert few sample documents into your collection
with the unique hash index for name
attribute first: 让我们首先使用
name
属性的唯一哈希索引将几个示例文档插入您的collection
:
FOR doc in [
{ "name": "Doc 1", "value": 1 },
{ "name": "Doc 2", "value": 1 },
{ "name": "Doc 3", "value": 1 }]
INSERT doc IN collection
Now if you want to perform a batch upsert you can run the following AQL: 现在,如果要执行批量增补,可以运行以下AQL:
FOR doc in [
{ "name": "Doc 2", "value": 2 },
{ "name": "Doc 3", "value": 2 },
{ "name": "Doc 4", "value": 1 }
]
UPSERT { "name": doc.name }
INSERT doc
UPDATE { "value": doc.value } in collection
AQL query above inserts one new Doc 4
document and updates value attribute for the Doc 2
and Doc 3
. 上面的AQL查询会插入一个新的
Doc 4
文档,并更新Doc 2
和Doc 3
value属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.