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.
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.
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.
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With