Update document via REST API like UPDATE table SET field=value?


#1
I think to update a document can not only provide the changed key-value such as UPDATE table SET field=value.
because the document is so flexible, the server don't know what you really want to do -- do you want to modify an exist value of key? or do you want to add some key-values? or do you want to delete some key-values? 
so the best way to update a document is to replace the whole document. though this need at least 3 steps:
  1. get the whole document and rev info.

  2. merge the update key-value you wanted into whole document.

  3. put changed whole document and rev info to update the document.

    Am I right?


#2

That’s the theory behind it, but in practice it’s a bit simpler. You can use the Update() method to take care of all of that stuff in one step.


#3

Thank you for your replying. I want to know that does REST API have the same Update() method?


#4

Oops, I didn’t realize the REST part in your title. You mean the REST API for Sync Gateway?


#5

Yes. If you look at the REST API docs, you’ll see that they let you GET and PUT entire documents, not individual fields. So to update a document you need to GET it, change any properties you want, then PUT the new version. (Make sure to preserve the _rev property; the PUT handler checks it to ensure you’re updating the current revision and not one that’s been replaced.)


#6

Thank you very much.