Search code examples
gremlintinkerpoptinkerpop3gremlin-server

Gremlin: How can I omit empty elements from the output


I have a gremlin query that returns this:

[Michael,Cleant,Jhonen]
[Michael,Cleant,Jhonen]
[]

What can I add to the query to remove an empty array element from the output?

To create this output you can use this code in the gremlin console:

g.addV("user").property("name", "Michael")
g.addV("user").property("name", "Cleant")
g.addV("user").property("name", "Jhonen")

g.V().values("name").fold().store("a").V().values("name").fold().store("a").select("a").V().hasLabel("nonExistent").fold().store("a").select("a").unfold()

Solution

  • You can just count the results of each by appending the following to the end of your traversal:

    where(count(local).is(gt(0)))
    

    or perhaps better as it would explicitly avoid a full count of all your lists:

    where(unfold())