merge_v in gremlin_python: adding custom ID

65 views
Skip to first unread message

Warwick Matthews

unread,
Feb 22, 2024, 2:15:22 AMFeb 22
to Gremlin-users
I am using a pattern where I match on label and name, and if there is no match then I want to create a vertex with a custom ID.

I have noticed in normal Groovy that this is done thus:
g.mergeV([(label):'person', 'name': "Wozza"]).option(onCreate, [(label): 'person', (id): 'XX-123-456-789']).next()

How would this be achieved via gremlin_python?  I can do the basic merge...
g.merge_v({T.label:'person' , name':'Wozza']})

... but I have been unable to create working code for "onCreate" functionality.


Cole Greer

unread,
Feb 22, 2024, 7:23:41 PMFeb 22
to Gremlin-users
Hi Warwick,

I believe this is what you need:
from gremlin_python.process.traversal import Merge, T
g.merge_v({T.label:'person', 'name': 'Wozza'}).option(Merge.on_create, {T.label: 'person', T.id: 'XX-123-456-789'}).next()

Let me know if you have any issues with this.

Regards,

Cole Greer

Warwick Matthews

unread,
Feb 24, 2024, 4:04:40 PMFeb 24
to Gremlin-users
Thank you!  I also realised I needed to include "from gremlin_python.process.traversal import Merge".

in pyMongo when I assign a variable to this query can the variable then be interrogated to understand if there was a match?


Ken Hu

unread,
Mar 1, 2024, 2:57:58 AMMar 1
to Gremlin-users
I'm not familiar with pyMongo, but I believe you are asking whether the mergeV step returns the matching vertex. MergeV will return the matching vertices, or if nothing matched, then it will return the newly created vertex. It isn't simple to distinguish between the two. Another wrinkle here is that if you wanted to see all the properties of the returned vertex then you might need to send an additional query to get that information. Prior to version 3.7.0, properties of vertices wouldn't be returned by default (a performance optimisation). Another consideration is that if there are multiple matches then you would want to do a to_list() or a loop of next() to get all the matches.
Reply all
Reply to author
Forward
0 new messages