Dear Wiki user, You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.
The "View_Snippets" page has been changed by GregTappero. The comment on this change is: added Sort values with reduce. http://wiki.apache.org/couchdb/View_Snippets?action=diff&rev1=23&rev2=24 -------------------------------------------------- This page collects code snippets to be used in your [[Views]]. They are mainly meant to help get your head around the map/reduce approach to accessing database content. Keep in mind that the the Futon web client silently adds group=true to your views. + * [[#common_mistakes|Common mistakes]] + * [[#get_doc_id|Get docs with a particular user id ]] + * [[#get_doc_with_attachment|Get all documents which have an attachment ]] + * [[#count_doc_with_attachment|Count documents with and without an attachment]] + * [[#list_unique_values|Generating a list of unique values]] + * [[#top_n_tags|Retrieve the top N tags]] + * [[#aggregate_sum|Joining an aggregate sum along with related data ]] + * [[#summary_stats|Computing simple summary statistics (min,max,mean,standard deviation) ]] + * [[#interactive_couchdb|Interactive CouchDB Tutorial]] + * [[#sort_values_1|Sort values with reduce]] + + <<Anchor(common_mistakes)>> == Common mistakes == When creating a reduce function, a re-reduce should behave in the same way as the regular reduce. The reason is that CouchDB doesn't necessarily call re-reduce on your map results. Think about it this way: If you have a bunch of values V1 V2 V3 for key K, then you can get the combined result either by calling reduce([K,K,K],[V1,V2,V3],0) or by re-reducing the individual results: reduce(null,[R1,R2,R3],1). This depends on what your view results look like internally. + <<Anchor(get_doc_id)>> == Get docs with a particular user id == {{{ @@ -20, +33 @@ Then query with key=USER_ID to get all the rows that match that user. + <<Anchor(get_doc_with_attachment)>> == Get all documents which have an attachment == This lists only the documents which have an attachment. @@ -34, +48 @@ In SQL this would be something like {{{SELECT id FROM table WHERE attachment IS NOT NULL}}}. - + <<Anchor(count_doc_with_attachment)>> == Count documents with and without an attachment == Call this with ''group=true'' or you only get the combined number of documents with and without attachments. @@ -64, +78 @@ In SQL this would be something along the lines of {{{SELECT num_attachments FROM table GROUP BY num_attachments}}} (but this would give extra output for rows containing more than one attachment). + <<Anchor(list_unique_values)>> == Generating a list of unique values == Here we use the fact that the key for a view result can be an array. Suppose you have a map that generates (key, value) pairs with many duplicates and you want to remove the duplicates. To do so, use ([key, value], null) as the map output. @@ -107, +122 @@ If you then want to know the total count for each parent, you can use the ''group_level'' view parameter: ''startkey=[''''''"thisparent"]&endkey=["thisparent",{}]&inclusive_end=false&group_level=1'' + <<Anchor(top_n_tags)>> == Retrieve the top N tags. == This snippet assumes your docs have a top level tags element that is an array of strings, theoretically it'd work with an array of anything, but it hasn't been tested as such. @@ -205, +221 @@ When querying this reduce you should not use the `group` or `group_level` query string parameters. The returned reduce value will be an object with the top `MAX` tag: count pairs. + <<Anchor(aggregate_sum)>> == Joining an aggregate sum along with related data == Here is a modified example from the [[View_collation|View collation]] page. Note that `group_level` needs to be set to `1` for it to return a meaningful `customer_details`. @@ -241, +258 @@ } }}} - + <<Anchor(summary_stats)>> == Computing simple summary statistics (min,max,mean,standard deviation) == Implementation in {{{JavaScript}}} by MarcaJames. Mistakes in coding are my fault, algorithms are from others, as noted. To the best of my knowledge the algorithms are public domain, and my implementation freely available to all (Perl Artistic License if you really need a license to consult) @@ -594, +611 @@ ||`["Tue", "08:00:00"] `|| ` {"S": 1276.8988123975391, "Sum": 1257.4497350063903, "M": 955, "min": 0.033031734767263086, "max": 6.011336961717487,` `"variance_n": 1.3370668192644388, "mean": 1.3167012932004087,` `"knuthOutput": {"M2": 1276.898812397539, "n": 955, "mean": 1.3167012932004083, "min": 0.033031734767263086, "max": 6.011336961717487,` `"variance_n": 1.3370668192644386}} `|| ||`["Tue", "08:05:00"]`||` {"S": 1363.1444727834003, "Sum": 1303.08214106713, "M": 939, "min": 0.03216066554751794, "max": 5.93544645899576,` `"variance_n": 1.4516980540824285, "mean": 1.387733909549659,` `"knuthOutput": {"M2": 1363.1444727834005, "n": 939, "mean": 1.3877339095496595, "min": 0.03216066554751794, "max": 5.93544645899576,` `"variance_n": 1.4516980540824287}} `|| + <<Anchor(interactive_couchdb)>> == Interactive CouchDB Tutorial == See [[http://labs.mudynamics.com/2009/04/03/interactive-couchdb/|this blog post]], which is a CouchDB emulator (in JavaScript) that explains the basics of map/reduce, view collation and querying CouchDB RESTfully. + <<Anchor(sort_values_1)>> + == Sort values with reduce == + + '''Map''' + + {{{ + function(doc) { + emit(doc.name, doc.int_list); + } + }}} + + "alice" [1,5,3] + + "bob" [6,2,8] + + "bob" [5,6,4,12] + + We want to get the following output: + + "alice" [1, 3, 5] + + "bob" [2, 4, 5, 6, 6, 8, 12] + + '''Reduce''' + + {{{ + function(keys, values, rereduce) + { + var int_list = new Array(); + + if(!rereduce) { + + // values is the list of matching map keys (user) values (int_list). + + for(var v in values) { + + // we loop the int in int_list + + for (var t in values[v]) { + + // append the int element to int_list + + int_list.push(values[v][t]); + + } + } + } + else + { + // in rereduce thats values[0] is the output return int_list + // of a previous reduce. + + int_list = values[0]; + + for(var v in values) { + + for (var t in values[v]) { + + int_list.push(values[v][t]); + + } + } + } + + function sort_int(a, b) {return a - b;} + + /* + if sort_int is less than 0, place a before b. + if sort_int returns 0, leave a and b unchanged in respect to eachother. + if sort_int is greater than 0, place a after b. + */ + + int_list.sort(sort_int); + + return int_list; + } + }}} +
