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
Recent Posts
2025-03-15
2025-03-03
2025-02-13
2025-01-14
2025-01-10