0

I'm adding keys to a graph using Gremlin. I have many key vertices all with a unique UUID.

Getting a key works like this:

g.V().hasLabel("key").property("uuid", "foobar").count()

or

g.V().hasLabel("key").property("uuid", "foobar")

Regardless what I fill in for foobar it always returns an object and count is always =1.

How is this possible?

Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101

1 Answers1

0

Neither of those Gremlin snippets involve "Getting a key" - they are actually setting all vertices with the label "key" to have a property called "uuid" with a value of "foobar". Perhaps that's what you meant?

Either way, you get a count() of "1" because your traversal returns the vertex on which you set the property and presumably you only have one vertex in your graph with the "key" label.

In case that's not what you meant, to get a value of a key you would instead do:

g.V().hasLabel("key").properties("uuid","foobar").count()

Assuming both "uuid" and "foobar" were property keys on the single vertex with the "key" label you would get a count() of "2" returned.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • O boy, that's obvious... What would be the main difference between `.has()` and `. properties ()`. Btw, thanks :-) – Bob van Luijt Jul 11 '18 at 20:44
  • 1
    `has()` is a filter function, meaning "find me elements with some condition" and `properties()` is more of a map function, meaning "for the current element get the `Property` objects from it. you should try to think of each step in a line of Gremlin as having some object (i..e a `Traverser`) passing through it and then that object is filtered, transformed, branched, etc. – stephen mallette Jul 11 '18 at 23:13
  • 1
    So pick apart `g.V().has('person','name','marko').properties()`. First step is `V()` which means "start traversal with all vertices". At the `has()` you imagine the first vertex from `V()` passing through. `has()` is a filter and this filter says, only allow through vertices if it has a "person" label and the "name" property of "marko". Then, for `properties()` we know we only have "marko" vertices reaching this step and and we know `properties()` is a type of map (i.e. transform) step. So you won't get a "marko" vertex for a result but instead all of that vertex's properties (name, age, etc) – stephen mallette Jul 11 '18 at 23:16
  • Thanks, this is _very_ helpful. – Bob van Luijt Jul 12 '18 at 07:48