简体   繁体   中英

How to permanently unset an attribute of ArangoDB document?

I want to remove an attribute from a document in ArangoDB.

I thought the correct method for this was with the function 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.

The simple answer to this is to combine UNSET with the REPLACE function.

UNSET works on attributes in a single document, and REPLACE works on entire documents in a collection.

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 .

The NEW and OLD keywords can be used as with UPDATE and 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:

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. Only the attributes specified after WITH are touched.

Note: if you explain the query, you will see nullMeansRemove: true under Write query options instead of keepNull: false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM