Search code examples
dgraphdgraph-dgo

How to update information in an existing node instead of creating a new one using Dgraph?


I am writing a Golang application using Dgraph for persisting objects. From the documentation, I can infer that a new UID and hence a new node is created everytime I mutate an object/run the code.

Is there a way to update the same node data instead for creating a new node?

I tried changing the UID to use "_:name" for the UID field but even this creates a new node everytime the application is run. I wish to be able to update the existing node if it is already present in the DB instead of creating a new node for it.


Solution

  • Unfortunately the docs aren't very beginner friendly yet :/

    To modify / mutate existing data you have to run a set operation and supply a rdf-triple like <uid> <predicate> "value" / <objectYouWantToModify> <attributeYouWantToModify> "quotedStringValue". If it is not an attribute but an edge, the value has to be another <uid>.

    The full mutation would be for example

    {
      set {
        <0x2> <name> "modified-name" .
      }
    }
    

    The . terminates the sequence and there is an optional fourth parameter you can use to also assign a label.

    Check https://www.w3.org/TR/n-quads/ for further details.