1

I would like to make a query that works on an element. But sometimes this element doe not already exist. So the query shoudl create it on demand. Like in Java the Optional::orElseGet - method.

My explicit example ("propertyWithMetaProps" is a property with cardinality "list"):

g.V(id).properties("propertyWithMetaProps").hasValue(1).{{your answer to create a property with value 1 in case such property does not exist}}.property("key","value");

Im on a remote graph. So keep in mind that I dont have access to the actual graph object.

1 Answer 1

1

You may want to investigate using the mergeV() step for this:

g.mergeV([(T.id):'001', someprop:1]).    // creates vertex if not exist, matches if exist
    option(Merge.onCreate,[created:'2022-02-07']).  // creates new property if vertex not found
    option(Merge.onMatch,[updated:'2022-02-07']).   // creates new property if vertex found
    property('newprop',5)     // creates new property in either case

Details on the mergeE() and mergeV() steps can be found here: https://tinkerpop.apache.org/docs/current/reference/#mergeedge-step

Not the answer you're looking for? Browse other questions tagged or ask your own question.