[英]How to permanently unset an attribute of ArangoDB document?
I want to remove an attribute from a document in ArangoDB. 我想从ArangoDB中的文档中删除一个属性。
I thought the correct method for this was with the function UNSET(doc, attributeName1, ..., attributeNameN)
. 我认为正确的方法是使用函数UNSET(doc, attributeName1, ..., attributeNameN)
。 However, with this alone, nothing is changed in the database. 但是,仅凭这一点,数据库中没有任何变化。
Example: 例:
let target_key = "42"
FOR doc IN myCollection
FILTER doc._key == target_key
RETURN UNSET(doc, "myAttribute")
The example returns the original document without the attribute myAttribute
, but the new version is not saved to the database , so it seems this is only a projected copy. 该示例返回没有属性myAttribute
的原始文档,但新版本未保存到数据库 ,因此看起来这只是一个预计的副本。
The simple answer to this is to combine UNSET
with the REPLACE
function. 对此的简单回答是将UNSET
与REPLACE
功能结合起来。
UNSET
works on attributes in a single document, and REPLACE
works on entire documents in a collection. UNSET
单个文档中的属性, REPLACE
集合中的整个文档。
let target_key = "42"
FOR doc IN myCollection
FILTER doc._key == target_key
REPLACE UNSET(doc, "myAttribute") IN myCollection
RETURN NEW
This example returns the new document where myAttribute
is removed with UNSET
that is saved to the collection with REPLACE
. 此示例返回使用UNSET
删除myAttribute
的新文档,该文档使用REPLACE
保存到集合中。
The NEW
and OLD
keywords can be used as with UPDATE
and UPSERT
. NEW
和OLD
关键字可以与UPDATE
和UPSERT
。
I figured this out after I read this issue . 在我读完这个问题后,我想到了这一点 。 I felt this was a too simple task to get stuck on, but I hope this is of use to people after me. 我觉得这是一个太简单的任务,但我希望这对我之后的人有用。
Alternatively, you can set the attribute you want to remove to null
and use the keepNull
option: 或者,您可以将要删除的属性设置为null
并使用keepNull
选项:
LET target_key = "42"
FOR doc IN myCollection
FILTER doc._key == target_key
UPDATE doc WITH { myAttribute: null } IN myCollection
OPTIONS { keepNull: false }
RETURN NEW
Attributes with an explicit null
value in the document are kept. 保留文档中具有显式null
值的属性。 Only the attributes specified after WITH are touched. 仅触摸WITH之后指定的属性。
Note: if you explain the query, you will see nullMeansRemove: true
under Write query options instead of keepNull: false
. 注意:如果您解释查询,您将在Write query options而不是keepNull: false
下看到nullMeansRemove: true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.