Use of by()

Can somebody explain the usecase of by() function in gremlin in very simple language.
Solution
by() is a step modulator, meaning it modifies the step it is being applied to in some way by giving it some additional instruction. an easy example to see this with is groupCount():
gremlin> g.V().groupCount()
==>[v[1]:1,v[2]:1,v[3]:1,v[4]:1,v[5]:1,v[6]:1]

Calling groupCount() without modulation implies the default behavior of grouping on the incoming traverser (i.e. the current Vertex). Each Vertex is simply counted once as a result as they are each unique entities. If we want to change that grouping behavior, we modulate that step with by(), like:
gremlin> g.V().groupCount().by(label)
==>[software:2,person:4]

Now we're saying, go ahead and group count the vertices but use the label of the Vertex for the grouping. it is important to note that what by() does is dependent on the step to which it is applied (and that on its own it really doesn't do anything at all).
Was this page helpful?