Skip to content

Instantly share code, notes, and snippets.

@edgardo001
Forked from eiri/primer.md
Created July 23, 2019 15:34
Show Gist options
  • Save edgardo001/33dab43afee14128d8052ae27f2fcfb7 to your computer and use it in GitHub Desktop.
Save edgardo001/33dab43afee14128d8052ae27f2fcfb7 to your computer and use it in GitHub Desktop.
Edit CouchDB design document with curl

Edit CouchDB design document with curl

create new database

$ curl -X PUT http://localhost:5984/koi
{"ok":true}

create a single document

$ curl -X POST http://localhost:5984/koi -d '{"name":"alice","age":25}' -H'Content-Type:application/json'
{"ok":true,"id":"9bb525a9771672ab8578f82d01001702","rev":"1-d9b27c5ae36c3e7e25bfaab1fb912e90"}

create design document that uses documents' "name" attribute as a key

$ curl -X PUT http://localhost:5984/koi/_design/ddoc -d '{"views":{"name":{"map":"function(doc) { emit(doc.name); }"}}}' -H'Content-Type:application/json'
{"ok":true,"id":"_design/ddoc","rev":"1-30aab27827e54bda77a5ac9ae6b72f6c"}

confirm we have a new built index

$ curl http://localhost:5984/koi/_design/ddoc/_view/name
{"total_rows":1,"offset":0,"rows":[
{"id":"9bb525a9771672ab8578f82d01001b8b","key":"alice","value":null}
]}

save design document into file

$ curl http://localhost:5984/koi/_design/ddoc -o ddoc.json -s
$ cat ddoc.json
{"_id":"_design/ddoc","_rev":"1-30aab27827e54bda77a5ac9ae6b72f6c","views":{"name":{"map":"function(doc) { emit(doc.name); }"}}}

edit design document in file "ddoc.json" and make it also emit value of documents' "age" attribute

$ vi ddoc.json
{
  "_id": "_design/ddoc",
  "_rev": "1-30aab27827e54bda77a5ac9ae6b72f6c",
  "views": {
    "name": {
      "map": "function(doc) { emit(doc.name, doc.age); }"
    }
  }
}

:wq

update design document

$ curl -X PUT http://localhost:5984/koi/_design/ddoc --data @ddoc.json -H'Content-Type:application/json'
{"ok":true,"id":"_design/ddoc","rev":"2-969b8f6ac9911b15c7884d9d5d527093"}

confirm we have index updated and emitting both "name" and "age"

$ curl http://localhost:5984/koi/_design/ddoc/_view/name
{"total_rows":1,"offset":0,"rows":[
{"id":"9bb525a9771672ab8578f82d01001b8b","key":"alice","value":25}
]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment