gremlin-scala
gremlin-scala copied to clipboard
Create vertices if not exists and add relation
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?
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()