CouchDB基本操作

CouchDB為Apache專案,發展的部份相當健全,有用過JSON的人應該都知道JSON針對不同語言的實作上支援相當好,官方就提供許多語言的API版本,CouchDB也一樣有各種語言的API實作版本,因此開發者可以相當快速進入並熟悉操作,今天很高興有機會試用一下Apache CouchDB,找了一下網路上文獻,親手在MiCloud上建了一各環境來試試,建置的過程因為是在SmartOS上建立,過程非常簡單,就不贅述,其他環境的安裝與使用可以參考下面"相關參考資料"部份,都有非常詳細說明。
而關於SmartOS,安裝好後,相關檔案位置如下:
  • DB安裝位置為:/opt/local/share/couchdb
  • 設定檔位置:/opt/local/etc/couchdb/default.ini
  • 預設admin web: http://COUCHDB_ADDR:5984/_utils/
  • 服務操作:
    • 啓動:svcadm enable couchdb
    • 關閉:svcadm disable couchdb
    • 重啓:svcadm restart couchdb

相關參考資訊:
#Couch APIs
#Couch Rest API Documents

基本操作:
#在資料檔案($1)下面建立一筆資料(JSON檔案$2)
curl http://COUCHDB_ADDR:5984/$1/ -d @$2 -H "Content-Type: application/json"
curl http://COUCHDB_ADDR:5984/$1/ -d "{\"\name\":\"simonsu\"}" -H "Content-Type: application/json"
ex:
$ curl -u "username" http://COUCHDB_ADDR:5984/sys_props/ -d "{\"name\":\"simonsu\"}" -H "Content-Type: application/json"
Enter host password for user 'username': *****
{"ok":true,"id":"f3471fdb35cb331dc663c252d4000492","rev":"1-f39a7c6b6026c79c7d9b478ab8df662a"}


#取得目前存在之所有資料文檔
ex:
$ curl -u "username:password" http://COUCHDB_ADDR:5984/_all_dbs
["_replicator","_users","sys_props","sys_user","test_suite_reports"]

#取得資料庫版本
ex:
curl -u "username:password" http://COUCHDB_ADDR:5984
{"couchdb":"Welcome","version":"1.1.0"}


#取得特定資料庫($1)內文
ex:
$ curl -u "username:password" http://COUCHDB_ADDR:5984/sys_user{"db_name":"sys_user","doc_count":5,"doc_del_count":1,"update_seq":12,"purge_seq":0,"compact_running":false,"disk_size":102489,"instance_start_time":"1327305757798970","disk_format_version":5,"committed_update_seq":12}

#取得需要認證(username:$1)的資料庫($2)內文
curl -u "$1" -X GET http://COUCHDB_ADDR:5984/$2
ex:
$ curl -u "username" -X GET http://COUCHDB_ADDR:5984/sys_props
Enter host password for user 'username':
{"db_name":"sys_props","doc_count":1,"doc_del_count":0,"update_seq":3,"purge_seq":0,"compact_running":false,"disk_size":12373,"instance_start_time":"1327294182116036","disk_format_version":5,"committed_update_seq":3}


#取得某資料庫($1)內文件列表
ex:
$ curl -u "username:password" http://COUCHDB_ADDR:5984/sys_user/_all_docs
{"total_rows":5,"offset":0,"rows":[
{"id":"123","key":"123","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}},
{"id":"123456","key":"123456","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}},
{"id":"3203e8685839a8136cddd483cc000a6f","key":"3203e8685839a8136cddd483cc000a6f","value":{"rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0"}},
{"id":"3203e8685839a8136cddd483cc001512","key":"3203e8685839a8136cddd483cc001512","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}},
{"id":"3203e8685839a8136cddd483cc00198e","key":"3203e8685839a8136cddd483cc00198e","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}}
]}

#附加資料庫內容列表
ex:
$ curl -u "username:password" http://COUCHDB_ADDR:5984/sys_user/_all_docs?include_docs=true
{"total_rows":5,"offset":0,"rows":[
{"id":"123","key":"123","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"123","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"123456","key":"123456","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"123456","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"3203e8685839a8136cddd483cc000a6f","key":"3203e8685839a8136cddd483cc000a6f","value":{"rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0"},"doc":{"_id":"3203e8685839a8136cddd483cc000a6f","_rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0","name":"simon","_attachments":{"\u87a2\u5e55\u5feb\u7167 2012-01-22 \u4e0a\u53481.41.11.png":{"content_type":"image/png","revpos":2,"digest":"md5-EmkJ3uGGgHSUbbvSyJ9cHQ==","length":78270,"stub":true}}}},
{"id":"3203e8685839a8136cddd483cc001512","key":"3203e8685839a8136cddd483cc001512","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"3203e8685839a8136cddd483cc001512","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"3203e8685839a8136cddd483cc00198e","key":"3203e8685839a8136cddd483cc00198e","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"3203e8685839a8136cddd483cc00198e","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}}
]}

#取得某資料庫($1)中某資料id($2)
ex: 
$ curl -X GET http://COUCHDB_ADDR:5984/sys_user/3203e8685839a8136cddd483cc000a6f
{"_id":"3203e8685839a8136cddd483cc000a6f","_rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0","name":"simon","_attachments":{"\u87a2\u5e55\u5feb\u7167 2012-01-22 \u4e0a\u53481.41.11.png":{"content_type":"image/png","revpos":2,"digest":"md5-EmkJ3uGGgHSUbbvSyJ9cHQ==","length":78270,"stub":true}}}

#建立資料庫名稱為$1 (未認證不可新增資料庫)
curl -u "username" -X PUT http://COUCHDB_ADDR:5984/$1
ex:
$ curl -X PUT http://COUCHDB_ADDR:5984/sys_props
{"error":"unauthorized","reason":"You are not a server admin."}
$ curl -u "username" -X PUT http://COUCHDB_ADDR:5984/sys_props
Enter host password for user 'username':
{"ok":true}


#刪除一筆資料
curl -u "username:password" -X DELETE http://COUCHDB_ADDR:5984/$1/$2?rev=$3
ex:
curl -u "username:password" -X DELETE http://COUCHDB_ADDR:5984/sys_user/62f0eed1d52e61c929e580f90a00012e?rev=1-9f3d0835176fbffc6163e3819d7f503f
{"ok":true,"id":"62f0eed1d52e61c929e580f90a00012e","rev":"2-f5e8452a6729974a036d05fce15c8a51"}

#拷貝一筆資料($1:document, $2:record_id, $3: revision, $4: destination_id)
curl -X COPY http://COUCHDB_ADDR:5984/$1/$2?rev=$3 -H "Destination: $4"
ex:
#目前資料列表
$ curl -u "username:password" http://COUCHDB_ADDR:5984/sys_user/_all_docs
{"total_rows":4,"offset":0,"rows":[
{"id":"123","key":"123","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}},
{"id":"3203e8685839a8136cddd483cc000a6f","key":"3203e8685839a8136cddd483cc000a6f","value":{"rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0"}},
{"id":"3203e8685839a8136cddd483cc001512","key":"3203e8685839a8136cddd483cc001512","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}},
{"id":"3203e8685839a8136cddd483cc00198e","key":"3203e8685839a8136cddd483cc00198e","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"}}
]}
#資料複製
$ curl -u "username:password" -X COPY http://COUCHDB_ADDR:5984/sys_user/3203e8685839a8136cddd483cc00198e?rev=1-9f3d0835176fbffc6163e3819d7f503f -H "Destination: 123456"
{"id":"123456","rev":"1-9f3d0835176fbffc6163e3819d7f503f"}
#複製後檢視資料
$ curl -u "username:password" http://COUCHDB_ADDR:5984/sys_user/_all_docs?include_docs=true
{"total_rows":5,"offset":0,"rows":[
{"id":"123","key":"123","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"123","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"123456","key":"123456","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"123456","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"3203e8685839a8136cddd483cc000a6f","key":"3203e8685839a8136cddd483cc000a6f","value":{"rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0"},"doc":{"_id":"3203e8685839a8136cddd483cc000a6f","_rev":"6-f3bc3c4a281c24b4197d4aa2473f57a0","name":"simon","_attachments":{"\u87a2\u5e55\u5feb\u7167 2012-01-22 \u4e0a\u53481.41.11.png":{"content_type":"image/png","revpos":2,"digest":"md5-EmkJ3uGGgHSUbbvSyJ9cHQ==","length":78270,"stub":true}}}},
{"id":"3203e8685839a8136cddd483cc001512","key":"3203e8685839a8136cddd483cc001512","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"3203e8685839a8136cddd483cc001512","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}},
{"id":"3203e8685839a8136cddd483cc00198e","key":"3203e8685839a8136cddd483cc00198e","value":{"rev":"1-9f3d0835176fbffc6163e3819d7f503f"},"doc":{"_id":"3203e8685839a8136cddd483cc00198e","_rev":"1-9f3d0835176fbffc6163e3819d7f503f","name":"simon","pwd":"12341234"}}
]}

這個網誌中的熱門文章

Bash判斷參數是否存在

Node.js package : forever