Search code examples
orientdbgremlin

Gremlin: project all labels and count of each


I currently have a query that gives me the count of each label: g.V().group().by(label).by(count())

However this is resulting in a column for each label. I want to project two columns "Entity Type" and "Count" and count the number of each label. So far, this is all I have but it is incorrect:

g.V().project('Entity Type','Count')
.by(label)
.by(groupCount())

Solution

  • First, group().by(label).by(count) can be simplified to groupCount().by(label).

    To reshape the result you only need a simple projection:

    g.V().
      groupCount().
        by(label).
      unfold().
      project('Entity Type','Count').
        by(keys).
        by(values)