CouchDB – Create Document

To create a document in CouchDB database, you can either use CouchDB Web Interface or REST API.

Create 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 create a 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/

We will use Postman, to trigger a PUT request with the URL to create a document in CouchDB Database. You can use any other CLI or GUI tool of your choice.

CouchDB - Create Document - REST API

Let us check if the database is created.

Send a HTTP GET Request with the same URL as above.

CouchDB - GET document

The document has been created and present. You get the document as JSON in the response.

Create Document via CouchDB Web Interface

You can also create 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 create the document. Click on Create Document button.

CouchDB - Create Document - Web Interface

An editor appears with the "_id" field pre-populated. You can override the id value. Also, add the required fields to the JSON text and then click on Create Document button.

CouchDB - Create Document - JSON

Once you click on the Create Document button, if everything is good, a new document is created.

CouchDB - Saving 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.

Summary

In this CouchDB Tutorial, we learned to create a new document in CouchDB database via HTTP REST API and Web Interface provided by CouchDB.