Create database and document

Create database and document

You can to do these actions using Fauxton, by url http://<address of any node in cluster>:5984/_utils or using HTTP requests. I will not describe Fauxton, cause it's very simple and user-friendly tool, so we will do this actions using HTTP requests and curl:

Create database:

curl -X PUT http://172.17.0.2:5984/products

{"ok":true}

Get database info:

curl -X GET http://172.17.0.2:5984/products

{"db_name":"products","update_seq":"0-g1AAAAFTeJzLYWBg4MhgTmEQzMtPSY03dDA0N9IzNNcz0DPKAUoxJTIkyf___z8rkQGhyAihyBisKEkBSCbZo6vDMCzJAaQunrB5CSB19QTV5bEASYYGIAVUOp8YtQsgavfjdydE7QGI2vvEqH0AUQtybxYACjNbTg","sizes":{"file":33952,"external":0,"active":0},"purge_seq":0,"other":{"data_size":0},"doc_del_count":0,"doc_count":0,"disk_size":33952,"disk_format_version":6,"data_size":0,"compact_running":false,"instance_start_time":"0"}

List all databases. Send this request to each node:

curl -X GET http://172.17.0.2:5984/_all_dbs
["products","test"]

curl -X GET http://172.17.0.3:5984/_all_dbs
["products","test"]

curl -X GET http://172.17.0.4:5984/_all_dbs
["products","test"]

You will see that database was created on each node of cluster and will be accessible from any node.

Earlier I have created a database named test, let's delete it:

curl -X DELETE http://172.17.0.2:5984/test
{"ok":true}

Database test was deleted from other nodes, too.

Create product related document.

Request CouchDB server to generate UUID, that we will use as ID of our document:

curl -X GET http://172.17.0.2:5984/_uuids

{"uuids":["1838ea4502e4f497a7a3e84917000e34"]}

If you need more IDs add count=<number> as parameter in request, where number is count of UUIDs max value is 1000 per request:

curl -X GET http://172.17.0.2:5984/_uuids?count=10

{"uuids":["1838ea4502e4f497a7a3e84917001c69","1838ea4502e4f497a7a3e84917001f5f","1838ea4502e4f497a7a3e84917002baf","1838ea4502e4f497a7a3e84917002e0e","1838ea4502e4f497a7a3e849170038df","1838ea4502e4f497a7a3e84917003ab6","1838ea4502e4f497a7a3e8491700419a","1838ea4502e4f497a7a3e84917004cad","1838ea4502e4f497a7a3e84917005766","1838ea4502e4f497a7a3e849170061b3"]}

PUT our document to database, document data represented as JSON:

curl -X PUT http://172.17.0.2:5984/products/1838ea4502e4f497a7a3e84917000e34 -d '{"name":"RaspberryPI", "ICU": "RP-00001","description":"Small and almost cool computer", "Price": "$35"}'

{"ok":true,"id":"1838ea4502e4f497a7a3e84917000e34","rev":"1-af1e308dcc6ac6e948aca3402b39037f"}

Developers of CouchDB recommends to check, that your document was really saved. I think it is related to distributed structure of database and may have places situations when some nodes can not be accessible from others. So, let's check:

curl -X GET http://172.17.0.2:5984/products/1838ea4502e4f497a7a3e84917000e34

{"_id":"1838ea4502e4f497a7a3e84917000e34","_rev":"1-af1e308dcc6ac6e948aca3402b39037f","name":"RaspberryPI","ICU":"RP-00001","description":"Small and almost cool computer","Price":"$35"}

All fine, document was created. But I have made an errors: instead SKU I've added ICU and haven't added space between "Raspberry" and "Pi", so lets edit this document in the next post.


Comments

Popular Posts