Search code examples
graphgremlintinkerpoptinkerpop3

Gremlin query to select vertex based on count of out edges on a particular subset of out vertex


For example, there are two types of vertex in Graph [ Account , User]. Account vertexes have edges to user vertex denoting a list of users form an Account vertex. User vertex has two property ( name, phoneNumber). I want to select accounts who are connected to more than 2 users whose name starts with foo.

The output should not contain accounts who have more than 2 edges to User vertex but out of all those users, only 1 user's name starts with foo. At minima, 2 users should have name which starts with foo.


Solution

  • This query would do it:

    g.V().hasLabel("Account")
    .where(out().hasLabel("User")
    .has("name", startingWith("foo"))
    .count().is(gte(2)))