Updating Data

Moved from GitHub dgraph-js/9

Posted by JohannesHoffmann:

i haven’t found any way to update data via mutation. With Ratel it looks like this:

{
    set {
        <0xf9078> <password> "xyz" .
     }
 }

How to implement this with dgraph-js and json? Is there some documentation i haven’t found?

Cheers

MichelDiz commented :

in dgraph-js/examples/simple at master · dgraph-io/dgraph-js · GitHub you can find simple examples how to do it.

There’s no func for “Update”. You have to do a mutation using the same uid from the node created previously and the same predicate. Just overwrite it.

JohannesHoffmann commented :

Yes, but the question is how to use the same uid in the right way?
Is a js object like:

const p = {
    "0xf9078": {
        password: "xyz"
     }
}

but this one does not work.
In the examples is only explained how to create new data and not how to override data with the uid and the same predicate.

janardhan1993 commented :

you can do

{
"uid" : "0xf9078",
"password": "xyz"
}

MichelDiz commented :

This way you’re doing is a miss concept of how mutations work. The client will recognize the object you’re sending (the update as overwrite) if you write how Janardhan did. So You don’t need to send an object like u did. It’s seems logical, but not how mutations works.

Dgraph do not store data as Json. It’s translate it into triples stores (nquad store - RDF 1.1 N-Quads ) and vice versa. You have to always think in RFD model to deal with Dgraph.

const p = {
          "uid" : "0xf9078",
          "password": "xyz"
}

manishrjain commented :

With this representation, our aim is to make the interaction work like documents. You can pass in the properties, along with the id of the document, and Dgraph would internally do what it needs to store them. Querying works the same way.

JohannesHoffmann commented :

Thank you all it is working well. This was not clear for me because i tried to mutete a override like the documentation of dgraph and not like a document. Maybe it would be nice to include an update mutation into the example to avoid this kind of questions?