canbax
canbax

Reputation: 3856

How to check if the certain vertex or edge has a property efficiently?

I'm using java driver. I try to check if a property exists for a certain vertex/edge.

int id = 1;
String propStartName = "startDate";
g.V(id).properties(propStartName).tryNext().isPresent();

This looks working but it seems too long and non-efficient. Is there a better way?

Upvotes: 0

Views: 1289

Answers (2)

Nishika Tiku
Nishika Tiku

Reputation: 11

You can write the following query:

g.V(id).project('propStartName').by(coalesce(values('propStartName'),constant('')))

If the propStartName does not have any value(empty string), that means that property does not exist for that particular vertex.

Upvotes: 1

HadoopMarc
HadoopMarc

Reputation: 1566

You can use:

g.V(id).has("propStartName").hasNext();

The session below in the gremlin console shows how you can experiment with this.

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().elementMap()
==>[id:1,label:person,name:marko,age:29]
==>[id:2,label:person,name:vadas,age:27]
==>[id:3,label:software,name:lop,lang:java]
==>[id:4,label:person,name:josh,age:32]
==>[id:5,label:software,name:ripple,lang:java]
==>[id:6,label:person,name:peter,age:35]
gremlin> g.V(1).has("lang")
gremlin> g.V(1).has("lang").hasNext()
==>false
gremlin> g.V(1).has("name").hasNext()
==>true

Upvotes: 2

Related Questions