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 "View_Snippets" by JoanTouzet
Date Tue, 24 Jan 2012 19:49:31 GMT
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 JoanTouzet:
http://wiki.apache.org/couchdb/View_Snippets?action=diff&rev1=45&rev2=46

  = View Snippets =
  <<TableOfContents()>>
  
- 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. 
+ 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.
  
  '''Remember''': the Futon web client silently adds ''group=true'' to your views.
  
  == 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.
  
- 
  == Get docs with a particular user id ==
- 
  {{{#!highlight javascript
  // map
  function(doc) {
@@ -26, +23 @@

    }
  }
  }}}
- 
  Then query with ''key=USER_ID'' to get all the rows that match that user.
  
- 
  == Get all documents which have an attachment ==
- 
  This lists only the documents which have an attachment.
  
  {{{#!highlight javascript
@@ -42, +36 @@

    }
  }
  }}}
- 
  In SQL this would be something like {{{SELECT id FROM table WHERE attachment IS NOT NULL}}}.
  
- 
  == 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.
  
  {{{#!highlight javascript
@@ -57, +48 @@

      emit("with attachment", 1);
    }
    else {
-     emit("without attachment", 1); 
+     emit("without attachment", 1);
    }
  }
  }}}
- 
  {{{#!highlight javascript
  // reduce
  function(keys, values) {
     return sum(values);
  }
  }}}
- 
  Using curl you can call it like this:
  
  {{{
- curl -s -i -X POST -H 'Content-Type: application/json' 
+ curl -s -i -X POST -H 'Content-Type: application/json'
-   -d '{"map": "function(doc){if(doc._attachments) {emit(\"with\",1);} else {emit(\"without\",1);}}",

+   -d '{"map": "function(doc){if(doc._attachments) {emit(\"with\",1);} else {emit(\"without\",1);}}",
-   "reduce": "function(keys, values) {return sum(values);}"}' 
+   "reduce": "function(keys, values) {return sum(values);}"}'
    'http://localhost:5984/somedb/_temp_view?group=true'
  }}}
- 
  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).
  
- 
  == 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 {{{emit([key, value], null)}}} as the map output.
  
  Call this with ''group=true'' or you only get ''null''.
@@ -95, +81 @@

    }
  }
  }}}
- 
  {{{#!highlight javascript
  // reduce
  function(keys, values) {
     return null;
  }
  }}}
+ This will give you results like
  
- This will give you results like
  {{{#!highlight javascript
  {"rows":[
  {"key":["thisparent","thatlink"],"value":null},
  {"key":["thisparent","thatotherlink"],"value":null}
  ]}
  }}}
- 
- You can then get all the rows for the key ''"thisparent"'' with the view parameters ''startkey=[''''''"thisparent"]&endkey=["thisparent",{}]&inclusive_end=false''
+ You can then get all the rows for the key ''"thisparent"'' with the view parameters ''startkey=["thisparent"]&endkey=["thisparent",{}]&inclusive_end=false''
  
  Note that the trick here is using the key for what you want to make unique. You can combine
this with the counting above to get a count of duplicate values:
  
@@ -123, +107 @@

    }
  }
  }}}
- 
  {{{#!highlight javascript
  // reduce
  function(keys, values) {
     return sum(values);
  }
  }}}
+ 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''
- 
- 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''
  
  == Get contents of an object with specific attributes e.g. doc.objects.[0].attribute ==
- 
  {{{#!highlight javascript
  // map
  function(doc) {
@@ -144, +124 @@

    }
  }
  }}}
+ == Arbitrarily query against any document property ("uniview") ==
+ '''Note: This is not a generally recommended approach, as your view size will be (# of documents
* # of document fields). '''However, if application's query behaviour cannot be determined
ahead of time, disk space is essentially free, and you do not wish to add views later, this
may be your only option.
  
+ {{{#!highlight javascript
+ // map
+ function(doc) {
+   for (prop in doc) {
+     emit([prop, doc[prop]], null);
+   }
+ }
+ }}}
+ Query a specific property by using ''startkey=["property", 0]&endkey=["property", {}]''
.
+ 
+ As an example, for a database of person data, ''startkey=["age", 18]&endkey=["age",
{}]&include_docs=true'' to retrieve all legal adults
+ 

Mime
View raw message