【ArangoDb踩坑】新增Edge类型数据,from和to为document的_id

【ArangoDb踩坑】新增Edge类型数据,from和to为document的_id

场景

从collection A(Document类型)中查找数据a,从collection B(Document类型)中查找数据b。在集合C(Edge类型)中新增数据,_from为a,_to为b。


我一开始是这么写的

let fr=FIRST(FOR f IN A FILTER f.name=="node" RETURN f)
let t=FIRST(FOR t IN B FILTER t.name=="in" RETURN t)
UPSERT {"_from":fr,"_to":t} 
    INSERT {"_from": fr,"_to": t} 
    UPDATE {"_from":fr,"_to":t} IN Bind
    RETURN NEW

报错

Query: AQL: edge attribute missing or invalid (while executing)


正确写法

_from_to为document的_id

let fr=FIRST(FOR f IN alarm_trigger_field FILTER f.name=="node" RETURN f._id)
let t=FIRST(FOR t IN alarm_operator FILTER t.name=="in" RETURN t._id)
UPSERT {"_from":fr,"_to":t} 
    INSERT {"_from": fr,"_to": t,"cascadeDelete": false,"revCascadeDelete": false} 
    UPDATE {"_from":fr,"_to":t} IN Bind
    RETURN NEW

优化aql

经前辈指点,我上面的写法如果找不到双方的ci会报错,下面的优化写法较好。

FOR field IN alarm_trigger_field FILTER field.name == 'node'
    FOR operator IN alarm_operator FILTER operator.name == 'in'
        UPSERT {_from : field._id, _to : operator._id}
        INSERT {"_from": field._id, "_to": operator._id, "cascadeDelete": false, "revCascadeDelete": false}
        UPDATE {"_from": field._id, "_to": operator._id}
        IN Bind
        RETURN NEW
posted @ 2022-04-22 11:35  musecho  阅读(176)  评论(0编辑  收藏  举报