How to do MERGE method like cypher in gremlin

298 views Asked by At
g.addV('person').property('name','Tim')

I got V[0] node

g.addV('person').property('name','Tim')

I got V[1] node with with the exact same label and key value again.

I just want V[0],if there is V[0] exist, V[1] can not be added.

1

There are 1 answers

1
Kelvin Lawrence On BEST ANSWER

The recommended way to do this with Gremlin today is to use the coalesce pattern as follows:

g.V().has('person','name','Tim').
      fold().
      coalesce(unfold(),addV('person').property('name','Tim'))

This will return the existing vertex or if it does not exist will create it and return the new vertex.

You can read more about this in the Gremlin Recipes and in Practical Gremlin .