MergeE with a conditional on the value being set

I'm trying to use MergeE to create an edge if it does not exist (based on the label and in/out nodes) and if it does exist I want to update a modified timestamp on the edge only if the incoming timestamp is greater than what is on the edge, so far I've got something like:
g.mergeE([
(T.label): 'RUNS',
(to): 'nodeID',
(from): 'nodeID'
]).
option(onCreate, [
(from): 'nodeID',
(to): 'nodeID',
(T.id): 'someUUID',
'modifiedTime': 'Wed Mar 05 12:15:39 EST 2025'
]
).
option(onMatch, __.filter(__.coalesce(
__.values("modifiedTime").is(lt('Wed Mar 05 12:15:39 EST 2025')),
__.constant(true))).
property("modifiedTime", 'Wed Mar 05 12:15:39 EST 2025')).
next()
g.mergeE([
(T.label): 'RUNS',
(to): 'nodeID',
(from): 'nodeID'
]).
option(onCreate, [
(from): 'nodeID',
(to): 'nodeID',
(T.id): 'someUUID',
'modifiedTime': 'Wed Mar 05 12:15:39 EST 2025'
]
).
option(onMatch, __.filter(__.coalesce(
__.values("modifiedTime").is(lt('Wed Mar 05 12:15:39 EST 2025')),
__.constant(true))).
property("modifiedTime", 'Wed Mar 05 12:15:39 EST 2025')).
next()
but I can't seem to figure out what should be going into the onMatch section to get this to work
Solution:
Hi @thorOdinson, I can add some context which might be helpful here. The purpose of the "onMatch traversal" is not actually intended to directly modify the found edge. That traversal is meant to produce a map which defines any properties to be updated. In your case, you would want a traversal which produces ["modifiedTime" : 'Wed Mar 05 12:15:39 EST 2025'] if the timestamp needs to be updated, or produces an empty map [:] otherwise. Your approach of directly modifying the edge certainly can work, but I would give a caveat that this is not how the mergeE step was intended to be used. One thing you are missing if you want to continue with this approach is that the "onMatch" traversal must still produce a map. The simplest fix is to return an empty map if you don't want mergeE to make any changes beyond what is already done by your "match traversal". This might look something like this:...
Jump to solution
2 Replies
Solution
ColeGreer
ColeGreer7d ago
Hi @thorOdinson, I can add some context which might be helpful here. The purpose of the "onMatch traversal" is not actually intended to directly modify the found edge. That traversal is meant to produce a map which defines any properties to be updated. In your case, you would want a traversal which produces ["modifiedTime" : 'Wed Mar 05 12:15:39 EST 2025'] if the timestamp needs to be updated, or produces an empty map [:] otherwise. Your approach of directly modifying the edge certainly can work, but I would give a caveat that this is not how the mergeE step was intended to be used. One thing you are missing if you want to continue with this approach is that the "onMatch" traversal must still produce a map. The simplest fix is to return an empty map if you don't want mergeE to make any changes beyond what is already done by your "match traversal". This might look something like this:
option(onMatch, __.filter(__.coalesce(
__.values("modifiedTime").is(lt('Wed Mar 05 12:15:39 EST 2025')),
__.constant(true))).
property("modifiedTime", 'Wed Mar 05 12:15:39 EST 2025')
.constant([:]))
option(onMatch, __.filter(__.coalesce(
__.values("modifiedTime").is(lt('Wed Mar 05 12:15:39 EST 2025')),
__.constant(true))).
property("modifiedTime", 'Wed Mar 05 12:15:39 EST 2025')
.constant([:]))
I haven't taken a close look at that traversal to ensure it's doing what you want, but hopefully this helps explains what's going on and helps you move forward. If you're still having issues with this query, it would be helpful to know what version of TinkerPop are you using and is this with TinkerGraph or some other graph provider? It would also be helpful to know specifically what sort of error or unintended behaviour you're seeing.
thorOdinson
thorOdinsonOP6d ago
for sure! right now I'm executing against the gremlin-server v3.7.1 but will eventually be executing this against neptune, that's helpful context for "it needs to produce a map of properties to be updated" I think that's where I've been getting tripped up just wanted to update that the solution worked! thanks for clarifying that for me

Did you find this page helpful?