Is there an efficient way to see how many edges and verticies were created in a gremlin Upsert?

Viewed 26

Using a query like the following

g.V().has('person','name','marko').
           fold().
           coalesce(unfold(),
                    addV('person').property('name','marko')).
           property('age',29)

Is there an efficient way to also return how many verticies were created?

I.e. 0 in the case that the marko vertex exists, 1 in the case that the marko vertex does not exist.

1 Answers

I think I'd go with union():

gremlin> g.V().has('person','name','marko').
......1>   fold().
......2>   union(coalesce(unfold(),
......3>                  addV('person').property('name','marko')).
......4>         property('age',29),
......5>         count(local))
==>v[1]
==>1
gremlin> g.V().has('person','name','darko').
......1>   fold().
......2>   union(coalesce(unfold(),
......3>                  addV('person').property('name','marko')).
......4>         property('age',29),
......5>         count(local))
==>v[13]
==>0

It's the reverse of what you asked for in terms of 1 and 0 existence but I suppose that would be easy enough to remedy if you really needed to but it would add additional complexity to the traversal around that count(local) which is pretty easy to read right now as it is.

Related