Search code examples
gremlinamazon-neptune

gremlin - how to update vertex property with another vertex's property


testing data:

vertex A has property 'a' value '1'
vertex B has outEdge  'e' to A
vertex B had property 'b' value '2'

how do I update 'a' to be value from 'b' in this case '2'?

I have tried this but not working

g.V().hasLabel('A').property('a', inE('e').outV().project('b').by('b').unfold())

Solution

  • Building the graph using...

    gremlin> g.addV('A').property('a','1').as('a').
       ......1>   addV('B').property('b','2').as('b').
       ......2>   addE('e').from('b').to('a')
    
    gremlin> g.V().valueMap()
    ==>[a:[1]]
    ==>[b:[2]]
    

    You can use values from A to update B as follows (this is one way to write the query, there are alternative ways)

    gremlin> g.V().hasLabel('B').as('b').V().hasLabel('A').property('a',select('b').values('b'))
    ==>v[42790]
    
    gremlin> g.V().valueMap()
    ==>[a:[2]]
    ==>[b:[2]]