0

I am trying to check if vertex exists before creating a new one or updating existing one:

 x = g.V().has('name','xyz').hasNext()  // /search 1

 if ( x != true ){
    g.V().addVertex( 'name', 'xyz' ) }

 g.V().has('name','xyz').property('x','1')  // search 2

The example has 2 searches for element with name = xyz; I would like to save first search to a variable and refer to it later.

However, if I try to save search, it does not get saved:

 gremlin> x = g.V().has('name','xyz')
 ==>v[40964336]
 gremlin> x
 gremlin>

What is the way to avoid having two searches in this example? - thank you!!!

Mohamed Taher Alrefaie
  • 15,698
  • 9
  • 48
  • 66
alex_123
  • 211
  • 1
  • 2
  • 7

2 Answers2

2

Maybe you missed tryNext:

g.V().has('name','xyz').tryNext().orElseGet {
  graph.addVertex('name', 'xyz')
}.property('x', '1')
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
0

Found my own answer: collect()

 x= g.V().has('name','axdas').collect()

 if ( x.size() > 0 ) { //set property
   }

 else { // add vertex
   }
Mohamed Taher Alrefaie
  • 15,698
  • 9
  • 48
  • 66
alex_123
  • 211
  • 1
  • 2
  • 7