Gremlin upsert on a vertex but preventing updates on a particular property on a vertex during upsert
Hi, Following is an upsert query on a vertex with label 'stvertex' and I am required to initialize and default the 'flagProp' to 0 only during creation of this vertex but during update I am required to prevent any update only on 'flagProp' while rest of the property should update, below is one of the approach I took: g.V(id).fold().coalesce(unfold(), __.addV(‘stvertex’)
.property(T.id, id).property(Cardinality.single, flagProp,0))
.property(Cardinality.single, name, nameValue)
.property(Cardinality.single, station, stationValue)
.property(Cardinality.single, base, baseValue).next() The above query only works for creation of new vertex I am looking for some help to write the correct query which works for update as well
.property(T.id, id).property(Cardinality.single, flagProp,0))
.property(Cardinality.single, name, nameValue)
.property(Cardinality.single, station, stationValue)
.property(Cardinality.single, base, baseValue).next() The above query only works for creation of new vertex I am looking for some help to write the correct query which works for update as well
Solution
Hi Salman,
Better to use
You may set
Better to use
mergeV step.You may set
flagProp in onCreate option, and create/update all other properties in both onCreate and onMatch, something likegremlin> g.mergeV([(T.label):'stvertex']).
......1> option(Merge.onCreate,[flagProp:0, name:'test1']).
......2> option(Merge.onMatch,[name:'test2'])
==>v[17]
gremlin> g.V(17).elementMap()
==>[id:17,label:stvertex,name:test1,flagProp:0]
gremlin> g.mergeV([(T.label):'stvertex']).
......1> option(Merge.onCreate,[flagProp:0, name:'test1']).
......2> option(Merge.onMatch,[name:'test2'])
==>v[17]
gremlin> g.V(17).elementMap()
==>[id:17,label:stvertex,name:test2,flagProp:0]