Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to permanently unset an attribute of ArangoDB document?

Tags:

arangodb

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.

like image 516
Thomas Fauskanger Avatar asked Aug 28 '17 14:08

Thomas Fauskanger


People also ask

How do I delete a collection from ArangoDB?

You can for example retrieve the list of all collections (excluding system ones) and drop or truncate them. The latter will remove all documents and keep indexes. Alternatively you can use AQL REMOVE statement.

What is _REV in ArangoDB?

Document Revision Every document in ArangoDB has a revision, stored in the system attribute _rev . It is fully managed by the server and read-only for the user. Its value should be treated as opaque, no guarantees regarding its format and properties are given except that it will be different after a document update.


2 Answers

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.

like image 97
CodeManX Avatar answered Dec 19 '22 21:12

CodeManX


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.

like image 40
Thomas Fauskanger Avatar answered Dec 19 '22 21:12

Thomas Fauskanger