couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Couchdb Wiki] Update of "HttpViewApi" by ChrisAnderson
Date Wed, 28 May 2008 20:37:48 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.

The following page has been changed by ChrisAnderson:
http://wiki.apache.org/couchdb/HttpViewApi

The comment on the change is:
added some documentation for Reduce

------------------------------------------------------------------------------
  
  == Creating Views ==
  
- To create a permanent view, the functions must first be saved into special ''design documents''.
The IDs of design documents must begin with ''_design/'' and have a special views attribute
that holds all the view functions.
+ To create a permanent view, the functions must first be saved into special ''design documents''.
The IDs of design documents must begin with ''_design/'' and have a special views attribute
that have a ''map'' member and an optional ''reduce'' member to hold the view functions.
  
- A design document that defines ''all'' and ''by_lastname'' views might look like this:
+ A design document that defines ''all'', ''by_lastname'', and ''total_purchases'' views might
look like this:
  
  {{{
  {
    "_id":"_design/company",
    "_rev":"12345",
-   "language": "text/javascript",
+   "language": "javascript",
    "views":
    {
+     "all": {
-     "all": "function(doc) { if (doc.Type == 'customer')  emit(null, doc) }",
+       "map": "function(doc) { if (doc.Type == 'customer')  emit(null, doc) }"
+     },
+     "by_lastname": {
-     "by_lastname": "function(doc) { if (doc.Type == 'customer')  emit(doc.LastName, doc)
}"
+       "map": "function(doc) { if (doc.Type == 'customer')  emit(doc.LastName, doc) }"
+     },
+     "total_purchases": {
+       "map": "function(doc) { if (doc.Type == 'purchase')  emit(doc.Customer, doc.Amount)
}",
+       "reduce": "function(keys, values, combine) { return sum(values) }"
+     }
    }
  }
  }}}
  
- The ''language'' property tells CouchDB the content type of the view functions, which it
uses to select the appropriate ViewServer. The default is to assume Javascript, so this property
can be omitted for Javascript views.
+ The ''language'' property tells CouchDB the language of the view functions, which it uses
to select the appropriate ViewServer (as specified in your couch.ini file). The default is
to assume Javascript, so this property can be omitted for Javascript views.
  
  == Altering/Changing Views ==
  
@@ -106, +114 @@

  POST /some_database/_temp_view  HTTP/1.0
  Content-Length: 48
  Date: Mon, 10 Sep 2007 17:11:10 +0200
- Content-Type: text/javascript
+ Content-Type: application/json
  
+ {
- function(doc) { if (doc.foo=='bar') { emit(null, doc.foo); } }
+   "map" : "function(doc) { if (doc.foo=='bar') { emit(null, doc.foo); } }"
+ }
+ 
  }}}
  
  Could result in the following response:
@@ -155, +166 @@

  
  When creating views, CouchDB will check the syntax of the submitted JSON, but the view functions
themselves will not be syntax checked by the Javascript interpreter. And if any one of the
view functions has a syntax error, none of the view functions in that design document will
execute. Perhaps test your functions in a temporary view before saving them in the database.
  
+ There is a log function available in the views, which logs to the couch.log. It can be helpful
for debugging but hinders performance, so it should be used sparingly in production systems.
+ 
+ {{{
+ {
+   "map": "function(doc) { log(doc); }"
+ }
+ }}}
+ 

Mime
View raw message