Search code examples
group-bygraphqlgremlintinkerpop3

How to group by edge property and get vertex+edge properties in the result?


i am new to gremlin, here is what i am trying to do..

I have a 'product' vertex and a 'user' vertex. I have a product -> 'linkedto' -> user edge.. This edge has 2 properties 'type' and 'joinedon'. I am trying to fetch list of users who are linked to the product grouped by the 'type' property of the edge. The output should be something like this:

type: [

{name, email..., joinedon}, // user1 details + joinedon (from the edge)

{name, email..., joinedon}, // user2 details + joinedon (from the edge)

]

I have got to a point where i get grouped users with their details with this query


g.V(productid).outE('linkedto').as('a')
  .inV().as('b').group().by(select('a').values('type')).unfold()
  .project('type','user').by(select(keys)).by(select(values))

Questions:

  1. How can i get the joinedon from edge in result?
  2. How to select only specific fields from the edge and vertex (user) in the result?

Solution

  • I would do the projection first, then the grouping.

    g.V(1).outE('linkedto').
      project('type', 'name', 'email', 'joinedon').
        by(values('type')).
        by(inV().values('name')).
        by(inV().values('email')).
        by(values('joinedon')).
      group().
        by('type')