20

I am new to Gremlin query language. I have to insert data on a Cosmos DB graph (using Gremlin.Net package), whether the Vertex (or Edge) already exists in the graph or not. If the data exists, I only need to update the properties. I wanted to use this kind of pattern:

g.V().hasLabel('event').has('id','1').tryNext().orElseGet {g.addV('event').has('id','1')}

But it is not supported by Gremlin.Net / Cosmos DB graph API. Is there a way to make a kind of upsert query in a single query ?

Thanks in advance.

2 Answers 2

46

There are a number of ways to do this but I think that the TinkerPop community has generally settled on this approach:

g.V().has('event','id','1').
  fold().
  coalesce(unfold(),
           addV('event').property('id','1'))

Basically, it looks for the "event" with has() and uses fold() step to coerce to a list. The list will either be empty or have a Vertex in it. Then with coalesce(), it tries to unfold() the list and if it has a Vertex that is immediately returned otherwise, it does the addV().

If the idea is to update existing properties if the element is found, just add property() steps after the coalesce():

g.V().has('event','id','1').
  fold().
  coalesce(unfold(),
           addV('event').property('id','1')).
  property('description','This is an event')

If you need to know if the vertex returned was "new" or not then you could do something like this:

g.V().has('event','id','1').
  fold().
  coalesce(unfold().
           project('vertex','exists').
             by(identity()).
             by(constant(true)),
           addV('event').property('id','1').
           project('vertex','exists').
             by(identity()).
             by(constant(false)))

Additional reading on this topic can be found on this question: "Why do you need to fold/unfold using coalesce for a conditional insert?"

Also note that optional edge insertion is described here: "Add edge if not exist using gremlin".

As a final note, while this question was asked regarding CosmosDB, the answer generally applies to all TinkerPop-enabled graphs. Of course, how a graph optimizes this Gremlin is a separate question. If a graph has native upsert capabilities, that capability may or may not be used behind the scenes of this Gremlin so there may be better ways to implement upsert by way of the graphs systems native API (of course, choosing that path reduces the portability of your code).

UPDATE: As of TinkerPop 3.6.0, the fold()/coalesce()/unfold() pattern has been largely replaced by the new steps of mergeV() and mergeE() which greatly simplify the Gremlin required to do an upsert-like operation. Under 3.6.0 and newer versions, you would replace the first example with:

g.mergeV([(label): 'event', id: '1'])

or perhaps better, treat the property key named "id" as an actual vertex identifier of T (I've added the property key of "name" to help with the example):

g.mergeV([(label): 'event', (id): '1', name: 'stephen'])

The above will search for a vertex with the T.label, T.id and "name" in the Map. If it finds it, it is returned. If it does not find it, the Vertex is created using those values. If you have the T.id it may be even better to do:

g.mergeV([(id): '1']).
    option(onCreate, [(label): 'event', name: 'stephen'])

In this way, you limit the search criteria to just the identifier which is enough to uniquely identify it and avoids additional filters and then if the verted is not found the onCreate is triggered to use the supplied Map to create the vertex in conjunction with the search criteria Map.

5
  • What if I want the query to also tell me if the node already existed or was just created? What I came up with so far is : g.V('1234').fold().as('existing').coalesce(unfold(), addV().property(id,'1234')).as('result').select('existing','result') which works nicely, returning { 'existing': [], result: { ... } } when the node is created, but duplicates the returned payload if the node already exists: { 'existing': [{ /*node payload*/ }], result: { /*same node payload again*/ } } May 13, 2018 at 23:37
  • 1
    I think I found a better way (just learned out about store()): g.V('1235').fold().store('existing').coalesce(unfold(), addV().property(id,'1235').store('new')).cap('existing','new'). This way, either 'existing' or 'new' is always empty. Let me know if there's an even better way of exposing a boolean flag and a single property for the payload. May 13, 2018 at 23:53
  • your approach is good though it's nice to avoid side-effects if you can. updated my answer with a side-effect free traversal and a simple boolean value as the return for whether the vertex was added or not. May 14, 2018 at 10:29
  • That's great! Thank you for sharing. What about avoiding side effects? Is this due to the more philosophical quest for purity in functional programming, or does it have measurable, umm, side effects? May 14, 2018 at 10:33
  • 1
    It's nice to read Gremlin left to right without having to refer back to some earlier point in the traversal so from a readability perspective, I tend to think side-effects makes Gremlin harder to follow. You also incur additional processing and memory costs on your traversal for requiring side-effect constructs which is perhaps more of a reason to try to avoid them. May 14, 2018 at 10:43
1

Actually guys, here's how you do it!

This way you can Add/Update 2 Vertexes and an Edge and associate them at the same time!

g.V().
    has('Person', 'name', 'Ben').fold().
        coalesce(unfold().property('age', 25), 
        __.addV('Person').property('name','Ben').property('age',25)).store('start').
    V().has('Person','name','Robert').fold().
        coalesce(unfold().property('age', 41), 
        __.addV('Person').property('name','Robert').property('age',41)).
    coalesce(__.outE('link').has('id', 3).property('weight', 11), 
    __.addE('link').property('id', 3).property('weight', 10).to(select("start").unfold()))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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