How can I retrieve an instance and then update it with GRAQL?



I would like to do something really simple and I cannot find any documentation about how to do it with GRAQL. I tried several things and nothing worked - I would like to retrieve an entity instance by some attribute value, then update its value and persist it back . How is this possible with grakn / graql ?


Update existing data

Hello @attodorov,

we do not have update queries yet, so you will need to add the new attribute and delete the old one:

Say you want to change a person’s first name:

match $x has firstname "Robert"; insert $x has firstname "Marco";

and then delete the old attribute:

match $x has firstname $y; $y val "Robert"; delete $y;

EDIT: See Soroush’s answer for a more correct deletion. Often you want to only remove the association (relationship) to a specific Attribute, rather than deleting the actual attribute (which might be used by some other entity).

finally remember to commit to persist changes: commit;

You can also work with IDs to avoid ambiguity:

match $x has firstname "Robert"; get;

the response will be something like: $x id V90128 isa person;

then you can:

match $x id V90128; insert $x has firstname "Marco";
match $x id V90128 has firstname $y; $y val "Robert"; delete $y;


Delete attribute, bug or normal behavior?

To add to Marco’s answer, to ensure that you retain the instance of attribute firstname with value of "Robert" for anything else that may be owning it, use via when deleting.

match $x id V90128 has firstname "Robert" via $r; delete $r

In doing so, we’re deleting the association that $x has with the attribute firstname of value "Robert" , rather than the instance of the attribute itself.