CouchDB Updating Document

You can Update a document in CouchDB by sending an HTTP request to the server using PUT method through cURL utility.

Syntax

curl -X PUT http://127.0.0.1:5984/database_name/document_id/ -d 
'{ 
"field" : "value", "_rev" : "revision id" 
}'

Example :

The database with the "contacts" contains the document with ID johndoe. First you have to get the revision ID of the document which is to be updated. If you do not know _rev, you can find yourself in the document using the command below. Now after getting the revision ID, use _rev from the document to update the document. Now, we are updating the age of 25 to 30.

Find revision ID

$ curl -X GET http://127.0.0.1:5984/contacts/johndoe
{
   "_id" : "johndoe",
   "_rev" : "2-04d8eac1680d237ca25b68b36b8899d3 " ,
   "age" : "25"
}

-----------------------------------------------------
-----------------------------------------------------

Updating Age

$ curl -X PUT http://127.0.0.1:5984/contacts/johndoe/ -d
' { " age " : " 30 " , " _rev " : " 1-1c2fae390fa5475d9b809301bbf3f25e " } '

{ " ok " : true , " id " : " johndoe " , " rev " : " 2-04d8eac1680d237ca25b68b36b8899d3 " }



Verification :

If you want to Verify the document, you can verify using GET request.

$ curl -X GET http://127.0.0.1:5984/contacts/johndoe
{
   " _id " : " johndoe ",
   " _rev " : " 2-04d8eac1680d237ca25b68b36b8899d3 " ,
   " age " : " 30 "
 }



Visit :


Discussion


* You must be logged in to add comment.