Reputation: 23
How to write gremlin query which will do
Or is there any alternate way to override the vertex using gremlin query?
Upvotes: 1
Views: 738
Reputation: 46216
Using the TinkerPop modern graph as an example:
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],age:[29]]
and assuming full Gremlin support you could keep the "name" property (i.e. index property) remove all other properties and add new properties in a single line of Gremlin as follows:
gremlin> g.V().has('person','name','marko').
......1> sideEffect(properties().not(hasKey('name')).drop()).
......2> property('age',33).
......3> property('favoriteColor','red')
==>v[1]
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],favoriteColor:[red],age:[33]]
But in CosmosDB, I don't think you yet have support for the sideEffect()
step. The trick is that to do this in one traversal is that you need to "side-effect" the drop()
in some way and since drop()
behaves as a filter everything step, any steps you add after it will simply not execute as there is nothing left in the stream to operate on.
Some workaround ideas for the lack of sideEffect()
include using union()
with identity()
:
gremlin> g.V().has('person','name','marko').
......1> union(properties().not(hasKey('name')).drop(),
......2> __.identity()).
......3> property('age',33).
......4> property('favoriteColor','red')
==>v[1]
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],favoriteColor:[red],age:[33]]
Less readable than the intent of sideEffect()
but effective. I believe CosmosDB supports identity()
even though it is not documented on their web site. If you don't have identity()
you just have to get creative I suppose - here's another way which is even less nice:
gremlin> g.V().has('person','name','marko').as('a').
......1> union(properties().not(hasKey('name')).drop(),
......2> select('a')).
......3> property('age',33).
......4> property('favoriteColor','red')
==>v[1]
gremlin> g.V().has('person','name','marko').valueMap(true)
==>[id:1,label:person,name:[marko],favoriteColor:[red],age:[33]]
Upvotes: 2