Update Document in CouchDB

Update Document

Using Fauxton:

  • Open the following URL: http://127.0.0.1:5984/_utils/
  • Click on the edit option.
  • A new page will be displayed to edit the entries in a document.
  • Click on the save changes tab after editing.
  • The document is thus updated.

 

Using cURL utility:

Syntax:

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

Example:

Retrieve the revision id of the document to be updated.

curl -X GET http://127.0.0.1:5984/company/100

Use the revision id in the below command.

curl -H 'Content-Type: application/json' \

-X PUT http://127.0.0.1:5984/company/100/ -d'{"Salary": "40000",  "_rev":"1-609cd49b1a1bfa5423fcb712b5a65789"}'

 

Verification:

Verify the document updated using the below command:

curl -X GET http://127.0.0.1:5984/company/100

 

Important Points:

  • The database name and the document id must be present in the URL sent.
  • A field cannot be added to an existing document.
  • An entirely new version of a document can also be written in a database with the same document ID.
  • The response of the update will contain 3 fields:
    1. “ok”: It is used to specify that the operation is successful.
    2. “id”: It is used to store the id of the document.
    3. “rev”: It is used to specify the revision id. A _rev value is generated by CouchDB, every time when a document is revised (updated or modified).