1

We can update a vertex for example: g.V(vertex_id).property("name","Marko")
is there any way to replace a vertex?

jonyroy
  • 187
  • 1
  • 11

1 Answers1

4

So you want to replace all properties of one vertex by properties of another vertex (at least that's how I understand your question together with your comment).

To delete all properties you simply have to drop them:

g.V(vertex_id).properties().drop().iterate()

and we can see how to copy all properties from one vertex to another in this response by Daniel Kuppitz to a question on how to merge two vertices:

g.V(vertex_with_new_properties).
    sideEffect(properties().group("p").by(key).by(value())).
    cap("p").unfold().as("kv").
  V(vertex_id).
    property(select("kv").select(keys), select("kv").select(values)).
    iterate()

When we combine those two traversals, then we get a traversal that drops the old properties and copies over the new properties from the other vertex:

g.V(vertex_id).
    sideEffect(properties().drop()).
  V(vertex_with_new_properties).
    sideEffect(properties().group("p").by(key).by(value())).
    cap("p").unfold().as("kv").
  V(vertex_id).
    property(select("kv").select(keys), select("kv").select(values)).
    iterate()

In action for the modern graph:

// properties before for both vertices:
gremlin> g.V(1).valueMap(true)
==>{id=1, label=person, name=[marko], age=[29]}
gremlin> g.V(2).valueMap(true)
==>{id=2, label=person, name=[vadas], age=[27]}

// Replace all properties of v[1]:
gremlin> g.V(1).
            sideEffect(properties().drop()).
           V(2).
            sideEffect(properties().group("p").by(key).by(value())).
            cap("p").unfold().as("kv").
           V(1).
            property(select("kv").select(keys), select("kv").select(values)).
            iterate()

// v[1] properties after:
gremlin> g.V(1).valueMap(true)
==>{id=1, label=person, name=[vadas], age=[27]}
Florian Hockmann
  • 2,634
  • 13
  • 24