Jitesh
Jitesh

Reputation: 269

How to do partial update of a document

I need a guidance on how can I update a field in CouchDB. I tried curl via console it works fine but programatically. I don't understand how to update a particular field say 'name'. Here is the snippet of updating a document in CouchDB which works fine and returns me the updated revision id.

HttpPut httpPutRequest = new HttpPut(hostUrl +"/"+ docId);
StringEntity body = new StringEntity(jsonDoc.toString());
httpPutRequest.setEntity(body);

httpPutRequest.setHeader("Accept", "application/json");
httpPutRequest.setHeader("Content-type", "application/json");

Upvotes: 13

Views: 17689

Answers (2)

Thaina Yu
Thaina Yu

Reputation: 1512

It is possible to support partial updates by writing your own update function.

To be clear, it not actually a truly partial update. It still update the whole target document into new revision. But the update was done directly on the database itself. And so you could specified update partially on client side instead of retrieving and sending the whole document

Upvotes: 9

Ryan Duffield
Ryan Duffield

Reputation: 19029

Partial updates are not supported by CouchDB. In other words, to update a field in the document, you must update the field in your local JSON document and push that document to CouchDB as a whole.

You can accomplish this by still issuing an HTTP PUT, ensuring the appropriate _rev is included in your document.

More details are available in the wiki.

Upvotes: 24

Related Questions