0

From a traverser, if I just want the first item in the list of vertexes, how would I go about returning it as an object?

I've tried:

g.V()
.has("Project", "id", eq("someid"))
.outE("Contains")
.inV()
.hasLabel("Goal")
.sample(1)
.values("name")

Also tried:

g.V()
.has("Project", "id", eq("someid"))
.outE("Contains")
.inV()
.hasLabel("Goal")
.limit(1)
.values("name")

I've also tried fold but none of them worked for me. Any ideas?

1 Answers1

1

I'm not quite following what you want but your either traversal should only return one "name" value and not a list of "name" values, though I think I'd prefer the second since you said you want the first item returned. I'd re-write it as follows though:

g.V().has("Project", "id", "someid").
  out("Contains").hasLabel("Goal").
  values("name")
  limit(1)

You just pasted some Gremlin here, but you can also next() the Iterator to get that single first object:

String name = g.V().has("Project", "id", "someid").
                ...
                limit(1).next()

If you're seeing some other behavior for some reason in the return values please update your question to include a sample data script (example) so that it's easy to reproduce in the Gremlin Console.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135