CouchDB – Update Document
To update a document in CouchDB database, you can either use CouchDB Web Interface or send a PUT request to the REST API of CouchDB.
Update Document via REST API
Send a HTTP PUT request with the following URL.
http://hostname/database_name/new_document_id/
We have our CouchDB running in our localhost. Hence, we shall use 127.0.0.1:5984
as hostname.
We will use an existing database named tutorialkart
.
Let us update document with id 0005
.
The resulting request URL that we have to use for PUT request will become,
http://127.0.0.1:5984/tutorialkart/0005/
Send all the fields with the updated values or original values along with _rev
field.
{
"_rev": "1-8a00a24fcd8e1fbf9922c489df210f38",
"topic": "PostgreSQL Tutorial",
"category": "RDBMS",
"topics": 22
}
Remember that you should send all the fields. If you choose not to send an existing field of the document, the document will be removed in the update.
You can get the _rev
filed value, by sending a GET request to get the document details. For details, refer CouchDB – GET Document.
We will use Postman, to trigger a PUT request with the URL to update a document in CouchDB Database. You can use any other CLI or GUI tool of your choice.
Let us check if the document is updated.
Send a HTTP GET Request with the same URL as above.
The document has been updated. You get the document as JSON in the response. For each update to the document, the revision field "_rev"
gets changed.
Update Document via CouchDB Web Interface
You can also update document in CouchDB database using Web Interface.
Open URL http://hostname/_utils/
in your browser. In this example, the URL will be http://127.0.0.1:5984/_utils/
.
Click on Databases tab present in the left panel. Click on the database in which you would like to update the document. To view the document, open Table view and click on the document.
When you click on the document, an editor will be opened with the JSON document.
Here you can change the values. Let us change the topics field and Save Changes.
You would see an information popup at the top saying ‘Saving Document.‘
The document details are updated. Also, the revision field, "_rev"
is updated to a new value as there is an update to the document.
Summary
In this CouchDB Tutorial, we learned to update a document in CouchDB database via HTTP REST API and Web Interface provided by CouchDB.