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:
get the whole document and rev info.
merge the update key-value you wanted into whole document.
put changed whole document and rev info to update the document.
Am I right?
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.
Thank you for your replying. I want to know that does REST API have the same Update() method?
Oops, I didn’t realize the REST part in your title. You mean the REST API for Sync Gateway?
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.)