Conditional INSERT or REMOVE

166 views
Skip to first unread message

Rob

unread,
Apr 5, 2022, 12:24:23 PM4/5/22
to ArangoDB
Hi,

I am trying to figure out if it is possible to conditionally, based on FILTER, INSERT a result into a collection or if the FILTER didnt pass, do a REMOVE on the same collection.

Is this possible?

Kerry Hormann

unread,
Apr 15, 2022, 7:03:37 PM4/15/22
to ArangoDB
Once you modify a collection, you cannot make further modifications to the same collection in the same query.  However, you can update a different collection using the results from the first insert (such as using NEW._key to update/insert/remove a record in another collection).

For instance, this fails (two inserts into the same collection):
INSERT { value: 'baz' }
    INTO a
LET an = NEW
INSERT { value: 'boo' }
    INTO a
LET bn = NEW
RETURN { an, bn }

Returns:
    ERROR: Query: AQL: access after data-modification by collection 'a' (while optimizing ast)


But this works just fine (results from insert into "a" are then inserted into "b"):
INSERT { value1: 'foo' }
    INTO a
LET an = NEW
INSERT MERGE(NEW, { value2: 'bar' })
    INTO b
LET bn = NEW
RETURN { an, bn }


RETURNS:
[{
    "an": { "_id": "a/123", "_key": "123", "_rev": "_eA7tofW---", "value1": "foo" },
    "bn": { "_id": "b/123", "_key": "123", "_rev": "_eA7tofW--_", "value1": "foo", "value2": "bar" }
}]
Reply all
Reply to author
Forward
0 new messages