Pranjal
Pranjal

Reputation: 508

How to append the count of child vertices in Gremlin?

Here's the graph I'm working on.

g = TinkerGraph.open().traversal()
first_generation = g.addV('person').property('id', '1').next()
second_generation = g.addV('person').property('id', '2').next()
third_generation = g.addV('person').property('id', '3').next()
third_generation_1 = g.addV('person').property('id', '4').next()
fourth_generation = g.addV('person').property('id', '5').next()
g.addE('child').from(first_generation).to(second_generation)
g.addE('child').from(second_generation).to(third_generation)
g.addE('child').from(second_generation).to(third_generation_1)
g.addE('child').from(third_generation).to(fourth_generation)

Here, I want to fetch the list of all people with the number of children they have.

[{'id': 1, 'children': 1}, {'id': 2, 'children': 2}]

I read about sideEffect but can't seem to append the result of sideEffect to the output.

Any suggestions on how we can achieve the desired output ?

Upvotes: 0

Views: 386

Answers (1)

stephen mallette
stephen mallette

Reputation: 46216

You should probably use project()

gremlin> g.V().hasLabel('person').
......1>   project('id','children').
......2>     by('id').
......3>     by(out('child').count())
==>[id:1,children:1]
==>[id:2,children:2]
==>[id:3,children:1]
==>[id:4,children:0]
==>[id:5,children:0]

It takes each vertex and transforms it into a Map of the specified keys. The by() modulators then specify what the values of those keys should be.

Upvotes: 2

Related Questions