Create Document in CouchDB

Create Document

Instead of tables, data in CouchDB are stored in the form of documents. We can create a document in CouchDB either using Fauxton or cURL Utility.

 

Using Fauxton:

  • Open the Fauxton URL: http://127.0.0.1:5984/_utils/
  • Choose the specific database.
  • Put your cursor on all documents tab.
  • Click on NEW DOC.
  • Fill the entries to be added to the document.
  • Click on the save changes tab.
  • The document is thus successfully created.

 

Using cURL Utility:

Syntax:

curl -X PUT http://127.0.0.1:5984/database_name/"id" -d ' { document} '

 

Parameters:

  • database name: It is used to specify the name of the database in which the document needs to be created.
  • Id: It is used to specify the document id.
  • -d: It is used to send the data/document through an HTTP request.
  • document: It is used to specify the data of the document.

Example:

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

-X PUT http://127.0.0.1:5984/company/"100" -d'{"Name":"Tom", "Salary":"30000" , "Designation" : "Engineer" }'

 

Explanation:

The response will contain 3 fields:

  • “ok”: It is used to specify that the operation is successful.
  • “id”: It is used to store the id of the document.
  • “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).

 

Verification:

Verify the document created using the below command:

curl -X GET http://127.0.0.1:5984/database_name/ID