Search code examples
datastaxgremlindatastax-enterprisetinkerpopdatastax-enterprise-graph

Gremlin: Add an edge and drop an edge in single gremlin query


how to add an edge and dropping an edge to same vertex type in single gremlin query.

say we have two types of vertex types A --is--> B now i have vertex which is connected to some other vertex of B.

I want to update the A's vertex to some other vertex of B.

currently, i am dropping the current B vertex and then adding the new vertex .


Solution

  • You can do it in one traversal using a sideEffect():

    gremlin> g.V().has('person','name','marko').as('m').
    ......1>   outE('knows').
    ......2>   filter(inV().has('person','name','vadas')).
    ......3>   sideEffect(drop()).
    ......4>   V().has('person','name','peter').
    ......5>   addE('knows').from('m')
    ==>e[13][1-knows->6]
    

    At line 1 we basically identify the edge we want to get rid of (i.e. a "knows" edge from "marko" to "vadas") and we drop() that on line 3. At line 4 we lookup the vertex to whom we wish to connect "marko" to now and then add the edge at line 5.