0

Say I do g.V().has("id", 1).valueMap().next()

The result is all in lists:

{
  "id": [1],
  "name" ["node1"]
}

How can I unfold all the inner lists so that it shows:

{
  "id": 1,
  "name" "node1"
}
drum
  • 5,416
  • 7
  • 57
  • 91

1 Answers1

3

I think you already answered your question in a sense - you use unfold()

g.V().has("id",1).
  valueMap().
    by(unfold())

That syntax only works on 3.4.0 when by() modulator was added to valueMap(). In earlier versions you can still do it but it's not as pretty:

g.V().has("id",1).
  valueMap().
  unfold().
  group().
    by(keys).
    by(select(values).unfold())

As you can see, you basically have to deconstruct the Map and then reconstruct it with group(). If you have multiple vertices you need to isolate the unfold() and so:

g.V().
  map(valueMap().
      unfold().
      group().
        by(keys).
        by(select(values).unfold()))
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • What are `keys` and `values` supposed to be? – drum Mar 31 '19 at 04:19
  • I found them in `Column`, but I can't figure out which class to get the inner `select` – drum Mar 31 '19 at 04:39
  • it's an anonymous traversal - you static import the double underscore class: `__`- http://tinkerpop.apache.org/docs/current/tutorials/gremlins-anatomy/#_anonymous_traversals – stephen mallette Mar 31 '19 at 22:59
  • Thanks. Got it to work. Just one small question. How do I apply this for a list of `valueMaps`? – drum Apr 01 '19 at 03:17
  • when you have collections of anything (i.e. like List) to apply Gremlin to each "thing" in the collection you just need to `unfold()` it first (though some steps do allow `local` in-place application like `order()`). – stephen mallette Apr 01 '19 at 11:28
  • My apologies but I don't understand the dsl well enough to accomplish this. Can you please provide the way to do this? I'm still trying to learn gremlin. – drum Apr 01 '19 at 13:56
  • 1
    i misunderstood your question i think when i commented, please see my updated answer - i think that's what you're asking for. – stephen mallette Apr 01 '19 at 14:35
  • Thanks. That's exactly what I needed. – drum Apr 01 '19 at 14:44