gremlin-scala icon indicating copy to clipboard operation
gremlin-scala copied to clipboard

Create vertices if not exists and add relation

Open mkeshav opened this issue 5 years ago • 1 comments

I have a use case where I want to look up if a vertex exists based on a property and if it does not create and add relationship to another vertex. how do i go about doing it?

mkeshav avatar May 02 '19 22:05 mkeshav

val maybeVertexToAdd = MyVertexCC()
val vertexToLinkTo = StepLabel[Vertex]()
val vertexExists = 
g.V
  .has(Key[String]("property"), "someValue")
  .fold() // https://stackoverflow.com/questions/49758417/cosmosdb-graph-upsert-query-pattern/49758568#49758568
  .coalesce(
    _.unfold(),
    _.V("idOfVertex") // can replace this with query to find "another vertex"
       .as(vertexToLinkTo)
       .addV(maybeVertexToAdd) // you might need to alias this, try and see
      .addE("link")
      .to(vertexToLinkTo)
  )
  .iterate()

bowofolaf avatar Sep 12 '19 02:09 bowofolaf