I assume your question is about project()
specifically and that alternatives like elementMap()
and valueMap()
don't work for you for some reason in this case of having to deal with non-existent keys. With project()
you provide the keys and those keys must have values at least until Gremlin 3.5.0 when null
will be supported as part of the language.
For now, with project()
you can account for the missing property with something like coalesce()
but it means providing some kind of default value when that property key is not present:
g.V().hasLabel('Person').
project('name','phone').
by('Name').
by(coalesce(values('Phone'),constant("")))
You could also filter()
those keys away that hold the constant()
I suppose, but your Gremlin just gets uglier from there.
I think that if I needed this specific form that you're looking for I'd probably look away from project()
where the key must exist. Examples might be:
gremlin> g.V().valueMap('name','age').by(unfold())
==>[name:marko,age:29]
==>[name:vadas,age:27]
==>[name:lop]
==>[name:josh,age:32]
==>[name:ripple]
==>[name:peter,age:35]
gremlin> g.V().elementMap('name','age')
==>[id:1,label:person,name:marko,age:29]
==>[id:2,label:person,name:vadas,age:27]
==>[id:3,label:software,name:lop]
==>[id:4,label:person,name:josh,age:32]
==>[id:5,label:software,name:ripple]
==>[id:6,label:person,name:peter,age:35]
gremlin> g.V().map(properties('name','age').group().by(key).by(value()))
==>[name:marko,age:29]
==>[name:vadas,age:27]
==>[name:lop]
==>[name:josh,age:32]
==>[name:ripple]
==>[name:peter,age:35]
gremlin> g.V().valueMap('name','age').map(unfold().group().by(keys).by(select(values).unfold()))
==>[name:marko,age:29]
==>[name:vadas,age:27]
==>[name:lop]
==>[name:josh,age:32]
==>[name:ripple]
==>[name:peter,age:35]
gremlin> g.V().
......1> project('name','age').
......2> by('name').
......3> by(coalesce(values('age'), constant(Integer.MIN_VALUE))).
......4> map(unfold().
......5> filter(select(values).is(neq(Integer.MIN_VALUE))).
......6> fold())
==>[name=marko,age=29]
==>[name=vadas,age=27]
==>[name=lop]
==>[name=josh,age=32]
==>[name=ripple]
==>[name=peter,age=35]