Rishit Ratan
Rishit Ratan

Reputation: 85

Selective unfolding of values from Gremlin (Neptune)

I have a vertex, say:

Vertex vertex = g.addV("person")
                .property("name", "x")
                .property(VertexProperty.Cardinality.list, "email", "[email protected]")
                .property(VertexProperty.Cardinality.list, "email", "[email protected]")
                .next();

I am fetching values from the vertex using:

g.V(vertex).valueMap(true).by(unfold()).next();

I get:

{id=f862aa64-70d3-4c85-9bd0-1c938fdc2dc8, label=person, name=x, [email protected]}

I wish to extract all the values of the property email, hence I do not want that apply unfold() to that property alone, how do I add this condition?

Also, I wish to do this in the one query itself.

Upvotes: 1

Views: 654

Answers (1)

noam621
noam621

Reputation: 2856

You can unfold the valueMap by the number of values:

g.V().valueMap().by(choose(
      count(local).is(eq(1)),
      unfold()
      identity()
    ))

example: https://gremlify.com/bpy7apfj4yncr

Or you can use a simple project:

g.V().project('name', 'email').
    by(values('name')).
    by(values('email').fold())

example: https://gremlify.com/bqatjfi3rmrmt

Upvotes: 2

Related Questions