Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking vertex property equivalency in Gremlin / TitanDB

Tags:

gremlin

titan

If I can check the value of a property like this: g.V().hasLabel('appUser').has('myId','1234').values('isPrivate') ==>false

But when I check if that value is false within an if, doesn't return what I would expect: if(g.V().hasLabel('appUser').has('myId','1234').values('isPrivate') == 'false'){'is false'}else{'is true'} ==>is true

Similarly, this also doesn't return what I would expect: if(!g.V().hasLabel('appUser').has('myId','1234').values('isPrivate')){'is false'}else{'is true'} ==>is true

How should I update this to perform a conditional check?

like image 900
Fook Avatar asked Dec 12 '25 09:12

Fook


1 Answers

.values() returns a Traversal, which is an Iterable (see TinkerPop 3.1.1 JavaDoc). You have to call .next() in order to actually retrieve the property value.

The provided query can be written like this.

if(!g.V().hasLabel('appUser').has('myId','1234').values('isPrivate').next()){'is false'}else{'is true'}

Notice the call to .next() right after .values('isPrivate').

When using the Gremlin Console, Traversal objects are automatically iterated (.iterate()), so what looks magical actually isn't. Mid-script (or outside of the Gremlin Console), you have to .next() or .iterate() the Traversal yourself in order to execute it, whether that Traversal is meant to retrieve elements (like the current use case) or mutate the graph.

like image 89
jbmusso Avatar answered Dec 15 '25 23:12

jbmusso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!