Default value when property is missing with project() step in Gremlin?

Viewed 1178

I have following graph:

g.addV('TEST').property(id, 't1')
g.addV('TEST').property(id, 't2').property('a', 1)

If I do: g.V('t2').project('a').by(values('a')) the traversal works and returns map with key a because property is there.

But if I have project step in my traversal like following: g.V('t1').project('a').by(values('a'))

Because a is missing it returns error, is there any way to return null or empty value in such case from by() step to avoid this error?

1 Answers

You can use coalesce():

gremlin> g.V().project('a').by(coalesce(values('a'),constant('default')))
==>[a:default]
==>[a:1]
Related