user1472423
user1472423

Reputation: 101

Gremlin, 1-to-N relationship query issue

I'm new to Gremlin. Struggling to get this one right. Any help would be really appreciated.

I have the Comments(C), Plans(P) and Users(U) enter code here data in below format.

C3 - CommentsOn -> P1
C2 - CommentsOn -> P1
C1 - CommentsOn -> P1

U2 - Likes -> C3
U4 - Likes -> C3
U1 - Likes -> C1
U1 - Likes -> C2

Now I need to get the data in below format

[
 {
  "Comment": C3,
  "LikedBy": [{U2},{U4}]
 },
 {
  "Comment": C2,
  "LikedBy": [{U1}]
 },
 {
  "Comment": C1,
  "LikedBy": [{U1}]
 }
]

Meaning, I need to get the list of comments and their corresponding likes.

Upvotes: 0

Views: 51

Answers (1)

stephen mallette
stephen mallette

Reputation: 46216

In the future, you might consider including a Gremlin script that creates a small sample dataset so that you can get a tested answer (example). Anyway, the answer here is to use project():

g.V().hasLabel('Comment').
  project('Comment','LikedBy').
    by().
    by(__.in('Likes').fold())

Upvotes: 1

Related Questions