Nandita Sahu
Nandita Sahu

Reputation: 89

Gremlin query to get the list of vertex not connected with any other vertex?

I tried something like this g.traversal().V().hasLabel(labelName).where(__.bothE().count().is(0)).toList(); it's not working

Upvotes: 0

Views: 821

Answers (1)

stephen mallette
stephen mallette

Reputation: 46226

I don't see why your approach wouldn't work - here's an example graph and as you can see it returns the right answer:

gremlin> g.addV('person').property('name','marko').as('m').
......1>   addV('person').property('name','stephen').as('s').
......2>   addV('person').property('name','alice').as('a').
......3>   addE('knows').from('m').to('s').iterate()
gremlin> g.V().where(bothE().count().is(0)).valueMap()
==>[name:[alice]]

Perhaps you should check your data more closely to be sure it contains the structure that you expect. While your way worked, I think I would take this approach:

gremlin> g.V().where(__.not(bothE())).valueMap()
==>[name:[alice]]

In this way, you don't force a count() of all the edges to detect the edge existence when you are just looking for the absence of any edges.

Upvotes: 1

Related Questions