Azeli
Azeli

Reputation: 748

Tinkerpop3/Gremlin. Find (A) Upsert (B) add Edge A to B

I am looking for an upsert functionality in Gremlin.

Client program has a stream of (personId, favoriteMovieNodeId) that need to query for the favoriteMovieNodeId's, then UPSERT a person Vertex and create the [favoriteMovie] edge.

this will create duplicate Person nodes:

g.V().has(label,'movies').has('uid',$favoriteMovieNodeId).as('fm')
.addV('Person').property('personId', $personId).addE('favMovie').to('fm')

Is there a way to check for existence of node based on properties before adding a node? I can't seem to find the documentation on this very basic graph function thats a part of every underlying graph db.

Upvotes: 1

Views: 719

Answers (1)

Daniel Kuppitz
Daniel Kuppitz

Reputation: 10904

If the movie is guaranteed to exist, then it's:

g.V().has('movies','uid',$favoriteMovieNodeId).as('fm').
  coalesce(V().has('Person','personId', $personId),
           addV('Person').property('personId', $personId)).
  addE('favMovie').to('fm')

Upvotes: 2

Related Questions