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 "Introduction_to_CouchDB_views" by BrianCandler
Date Thu, 07 Jan 2010 10:21:47 GMT
Dear Wiki user,

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

The "Introduction_to_CouchDB_views" page has been changed by BrianCandler.
The comment on this change is: emit value with linked _id.
http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views?action=diff&rev1=26&rev2=27

--------------------------------------------------

  
  For queries which are not meant to actually condense the amount of information you often
can live without a reduce function. A common strategy is to get the data you are interested
to select by in into the ''key'' part and then use ''startkey'' and ''endkey'' on the result.
  
+ == Keys and values ==
+ 
- == Equal keys ==
+ === Equal keys ===
  
  CouchDB actually stores the [key,docid] pair as the key in the btree. This means that:
   * you always know which document the key and value came from (it's exposed as the 'id'
field in the view result)
@@ -207, +209 @@

  algorithm = utc_random
  }}}
  
- == Lookup Views ==
+ === Lookup Views ===
  
  The second parameter of the ''emit()'' function can be ''null''. CouchDB then only stores
the [key,docid] in the view. You can use the view as a compact lookup mechanism and fetch
the document's details, if needed, in subsequent requests or by adding parameter ''include_docs=true''
  
+ === Linked documents ===
+ 
+ ''This is a new feature in couchdb trunk / 0.11''
+ 
+ If you emit an object value which has '''{'_id': XXX}''' then include_docs will fetch the
document with id XXX rather than the document which was processed to emit the key/value pair.
+ 
+ This means that if one document contains the ids of other documents, it can cause those
documents to be fetched in the view too, adjacent to the same key if required.
+ 
+ For example, if you have the following hierarchically-linked documents:
+ 
+ {{{
+ [
+ { "_id": "11111" },
+ { "_id": "22222", "ancestors": ["11111"], "value": "hello" },
+ { "_id": "33333", "ancestors": ["22222","11111"], "value": "world" }
+ ]
+ }}}
+ 
+ you can emit the values with the ancestor documents adjacent to them in the view like this:
+ 
+ {{{
+ function(doc) {
+   if (doc.value) {
+     emit([doc.value, 0], null);
+     if (doc.ancestors) {
+       for (var i in doc.ancestors) {
+         emit([doc.value, Number(i)+1], {_id: doc.ancestors[i]});
+       }
+     }
+   }
+ }
+ }}}
+ 
+ The result you get is:
+ 
+ {{{
+ {"total_rows":5,"offset":0,"rows":[
+ {"id":"22222","key":["hello",0],"value":null,
+   "doc":{"_id":"22222","_rev":"1-0eee81fecb5aa4f51e285c621271ff02","ancestors":["11111"],"value":"hello"}},
+ {"id":"22222","key":["hello",1],"value":{"_id":"11111"},
+   "doc":{"_id":"11111","_rev":"1-967a00dff5e02add41819138abb3284d"}},
+ {"id":"33333","key":["world",0],"value":null,
+   "doc":{"_id":"33333","_rev":"1-11e42b44fdb3d3784602eca7c0332a43","ancestors":["22222","11111"],"value":"world"}},
+ {"id":"33333","key":["world",1],"value":{"_id":"22222"},
+   "doc":{"_id":"22222","_rev":"1-0eee81fecb5aa4f51e285c621271ff02","ancestors":["11111"],"value":"hello"}},
+ {"id":"33333","key":["world",2],"value":{"_id":"11111"},
+   "doc":{"_id":"11111","_rev":"1-967a00dff5e02add41819138abb3284d"}}
+ ]}
+ }}}
+ 
+ which makes it very cheap to fetch a document plus all its ancestors in one query.
+ 
+ Note that the "id" in the row is still that of the originating document. The only difference
is that include_docs fetches a different doc.
+ 
- == Complex Keys ==
+ === Complex Keys ===
  
  Keys are not limited to simple values. You can use arbitrary JSON values to influence sorting.
See ViewCollation for the rules.
  

Mime
View raw message