TheJach.com

Jach's personal blog

(Largely containing a mind-dump to myselves: past, present, and future)
Current favorite quote: "Supposedly smart people are weirdly ignorant of Bayes' Rule." William B Vogt, 2010

Quick CouchDB Fun

I'm a fan of the command line, so when I found out I could just use curl to mess with CouchDB instead of Futon I was happy. If you want to create a new document with views under some database, you can do this:

curl -d @t.json -u un:pw -H "Content-Type: application/json" -X PUT http://dynamobi.cloudant.com/sw/_design/rar


This will take the contents of the file t.json (which is below) and send them off to CouchDB. It will create a new document called "rar" under the "sw" database. Here is t.json:

{

"views": {
"shipped4": {
"map": "function(doc) { if ( doc.order_line_item[15].PRODUCTNAME == '1936 Chrysler Airflow') emit(doc._id, {ProductName: doc.order_line_item[15].PRODUCTNAME} ); }"
}
}
}


I find it's much easier to use a json file for my data instead of trying to specify it directly on the command line. Because if you do the command line route, you have to use $'{etc.}' and make sure you backslash-escape your single quotes. Anyway, you can then read the data the map function produces with:

curl -u un:pw http://dynamobi.cloudant.com/sw/_design/rar/_view/shipped4


Pretty easy, right?


Posted on 2011-04-18 by Jach

Tags: couchdb, curl, databases, programming

Permalink: https://www.thejach.com/view/id/168

Trackback URL: https://www.thejach.com/view/2011/4/quick_couchdb_fun

Back to the top

Back to the first comment

Comment using the form below

(Only if you want to be notified of further responses, never displayed.)

Your Comment:

LaTeX allowed in comments, use $$\$\$...\$\$$$ to wrap inline and $$[math]...[/math]$$ to wrap blocks.