16

How do I update an existing property of a node in a graph using Gremlin?

The following method creates two properties "timestamp" with 2 different values, instead of updating the existing property "timestamp":

ContentGraph.g.addV('Filter').property('timestamp', new Date());
ContentGraph.g.V().hasLabel('Filter').property('timestamp', new Date());
PLASMA chicken
  • 2,777
  • 2
  • 15
  • 25
Auricopter
  • 161
  • 1
  • 4

1 Answers1

28

I am not sure which graph DB you are using but some, such as Amazon Neptune, have a default cardinality of set. You can override that cardinality setting using the Cardinality.single enum.

ContentGraph.g.addV('Filter').property('timestamp', new Date());
ContentGraph.g.V().hasLabel('Filter').property(single,'timestamp', new Date());

Hope that helps, Kelvin

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • 7
    Thanks. This works for Neptune! Tip: If you're using gremlin-javascript (or gremlin from npm), you need to use "gremlin.process.cardinality.single" instead of "single" – Auricopter Feb 25 '19 at 18:34
  • 1
    Glad it worked for you. Could you please accept the answer if you are happy with it so others can see the question is answered. Nice job adding the reminder about the cardinality class :-) – Kelvin Lawrence Feb 25 '19 at 18:55
  • This answer helped as I am using Neo4J embedded at home and Neptune on production staging. I have one more question. "How can I (re)set the default cardinality?" – Manabu Tokunaga Jul 13 '21 at 13:51
  • The default cardinality when using Amazon Neptune is always `set` and that cannot be changed. However, you can override/reset it on a per property basis using the `Cardinality.single` enum. Such as `property(single,'score',75)` – Kelvin Lawrence Jul 13 '21 at 14:11