CouchDB Creating Document

If you were working with a traditional relational database management system, at this point you would be defining the schema for your database and mapping the various tables, columns, and relationships required for a contact database.

With CouchDB, however, you don't have to do anything with it. CouchDB databases are schema-free, meaning that their structure is not strictly defined, and as a result you can change them to suit your needs on the fly.

Documents are CouchDB central data structure. In CouchDB the contents of the database will be stored as documents whereas in traditional relational databases the contents stored as tables. You can create a document using cURL utility.Each document in CouchDB has a unique ID and, in general, a UUID (Universally Unique IDentifier) is used. UUID is a random number that have least chance of creating a duplicate.

Creating a Document using cURL Utility

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

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

Here we have to send the following −
1. The name of the database-name
2. document id
3. −d option is used to send the document through HTTP request

{
Name : johndoe
age : 25
Designation : Engineer
}



Example :

Using the above given syntax if you want to create a document with id johndoe in a database with name contacts.

$ curl -X PUT http://127.0.0.1:5984/contacts/"johndoe " -d
'{ " Name " : " johndoe " , " age " :" 25 " , " Designation " : " Engineer " }'
{"ok":true,"id":"johndoe ","rev":"1-1c2fae390fa5475d9b809301bbf3f25e"}

You've just created a document with the document ID of johndoe. The CouchDB server has automatically generated a revision number and included this in its response. This must return a response consisting of three fields −
1. "ok", the operation was successful.
2. "id", this stores the id of the document.
3. "rev", this indicates the revision id.


Verification :

If you want to view the created document, you can get it.

$ curl -X GET http://127.0.0.1:5984/contacts/johndoe 
{
"_id": "johndoe",
"_rev": "1-3fcc78daac7a90803f0a5e383f4f1e1e",
"Name": "johndoe ",
"age": 25,
"Designation": "Engineer"
}



Visit :


Discussion



* You must be logged in to add comment.