CouchConf-Bangalore-Querying-with-Couchbase
 

Like this? Share it with your network

Share

CouchConf-Bangalore-Querying-with-Couchbase

on

  • 815 views

 

Statistics

Views

Total Views
815
Views on SlideShare
815
Embed Views
0

Actions

Likes
0
Downloads
14
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

CouchConf-Bangalore-Querying-with-Couchbase Presentation Transcript

  • 1. 1
  • 2. Queryingwith Couchbase 2
  • 3. A View Is: 3
  • 4. A View Is:A named pair of functions: a map function 3
  • 5. A View Is:A named pair of functions: a map function a reduce function (optional) 3
  • 6. A View Is:A named pair of functions: a map function a reduce function (optional)An entry in a design document 3
  • 7. A View Is:A named pair of functions: a map function a reduce function (optional)An entry in a design documentA disk file of indexed results 3
  • 8. A Map Function Is: 4
  • 9. A Map Function Is:Called with every database document 4
  • 10. A Map Function Is:Called with every database documentAn emitter of a key and a value 4
  • 11. A Reduce Function Is: 5
  • 12. A Reduce Function Is:Called once with the map results 5
  • 13. A Reduce Function Is:Called once with the map resultsA simplifier (it reduces map output) 5
  • 14. Example "President" Document (1 of 44) 6
  • 15. Example "President" Document (1 of 44){ "_id":"5d5f25254ef8fd62d6b9f2db642a8fc2", "_rev": "1-157b2928bec2def71485cc751af7de37", "type":"president", "presidency":1, "name":"George Washington", "wikipedia_entry":"http://en.wikipedia.org/wiki/George_Washington", "took_office":1789, "left_office":1797, "party":"Independent", "home_state":"Virginia"} 6
  • 16. Example "Event" Document (1 of 883) 7
  • 17. Example "Event" Document (1 of 883){ "_id":"5d5f25254ef8fd62d6b9f2db642a9f7d", "_rev": "1-9630b35932dedbd4d31138aaf3385847", "type":"event", "year":1791, "event":"The independent Vermont Republic becomes the 14th state"} 7
  • 18. Design Document{... "_id":"_design/design_document", "_rev": "1-9630b35932dedbd4d31138aaf3385847", "views": { "party_state_name": { "map":"function ... ", "reduce": " ... "}, "president_events": { "map":"function ... "}, "president_names": { "map":"function ... "}, "presidents": { "map":"function ... ", "reduce": " ... "}, "time_in_office": { "map":"function .... ", "reduce": " ... "}, "total_time_in_office": { "map":"function .... ", "reduce": " ... "} }, ...} 8
  • 19. Design Document special{... "_id":"_design/design_document", "_rev": "1-9630b35932dedbd4d31138aaf3385847", "views": { "party_state_name": { "map":"function ... ", "reduce": " ... "}, "president_events": { "map":"function ... "}, "president_names": { "map":"function ... "}, "presidents": { "map":"function ... ", "reduce": " ... "}, "time_in_office": { "map":"function .... ", "reduce": " ... "}, "total_time_in_office": { "map":"function .... ", "reduce": " ... "} }, ...} 8
  • 20. Design Document special you choose{... "_id":"_design/design_document", "_rev": "1-9630b35932dedbd4d31138aaf3385847", "views": { "party_state_name": { "map":"function ... ", "reduce": " ... "}, "president_events": { "map":"function ... "}, "president_names": { "map":"function ... "}, "presidents": { "map":"function ... ", "reduce": " ... "}, "time_in_office": { "map":"function .... ", "reduce": " ... "}, "total_time_in_office": { "map":"function .... ", "reduce": " ... "} }, ...} 8
  • 21. president_names 9
  • 22. Invoke a View 10
  • 23. Invoke a Viewcurl -X GEThttp://localhost:5984/presidents/_design/design_doc/_view/president_names 11
  • 24. Invoke a Viewcurl -X GEThttp://localhost:5984/presidents/_design/design_doc/_view/president_names{"total_rows":44,"offset":0,"rows":[ {"id":"...","key":1789,"value":"George Washington"}, {"id":"...","key":1797,"value":"John Adams"}, {"id":"...","key":1801,"value":"Thomas Jefferson"}, {"id":"...","key":1809,"value":"James Madison"}, ...]} emitting document ids are always included 11
  • 25. Invoke a View 12
  • 26. Invoke a View 12
  • 27. Under the Hood: Views CouchDBSpidermonkey Disk ICU 13
  • 28. Under the Hood: Viewshttp://localhost:5984/presidents/_design/design_doc/_view/president_names CouchDB Spidermonkey Disk ICU 13
  • 29. Under the Hood: Views Erlang HTTPhttp://localhost:5984/presidents/_design/design_doc/_view/president_names CouchDB Spidermonkey Disk ICU 13
  • 30. Under the Hood: Views Erlang HTTP mod_couchhttp://localhost:5984/presidents/_design/design_doc/_view/president_names CouchDB Spidermonkey Disk ICU 13
  • 31. Under the Hood: Views Erlang HTTP mod_couch query server CouchDB storage enginehost:5984/presidents/_design/design_doc/_view/president_names Spidermonkey Disk ICU 13
  • 32. Under the Hood: Views{"total_rows":44, "offset":0, "rows":[...]} Erlang HTTP mod_couch query server CouchDB storage engine view Spidermonkey Disk ICU 13
  • 33. Under the Hood: Views{"total_rows":44, "offset":0, "rows":[...]} CouchDB Spidermonkey Disk ICU 14
  • 34. Under the Hood: Views{"total_rows":44, "offset":0, "rows":[...]} {"id":"...","key":1789,"value":"George Washington"}, {"id":"...","key":1797,"value":"John Adams"}, ... CouchDB Spidermonkey Disk ICU 14
  • 35. Fetch Documents Matching a KeyGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?key=1993 any valid JSON 15
  • 36. Fetch Documents Matching a KeyGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?key=1993 total view rows offset into rows{"total_rows":44,"offset":41,"rows":[! ! {"id":"...","key":1993,"value":"Bill Clinton"}]} matching key 15
  • 37. Invoke a View Text 16
  • 38. Invoke a View Text 16
  • 39. Query Parameters in the Java Clientquery = new Query();query.setKey(1993);view = client.getView("design_doc", "president_names");future = client.asyncQuery(view, query);viewResponse = future.get(); 17
  • 40. Query Parameters in the Ruby ClientURL = ‘http://localhost:8091/pools/default’couchbase = Couchbase.new(URL)design_doc = couchbase.design_docs[‘design_doc’]design_doc.president_names(:key => 1993).fetch 18
  • 41. Query Parameters in the PHP Client$cb = new Couchbase;$cb->addCouchbaseServer("localhost");$view = $cb->getView("design_doc", "president_names");$result = $view->getResultByKey(1993)); 19
  • 42. Get a Key Range of DocumentsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?startkey=1790&endkey=1810 20
  • 43. Get a Key Range of DocumentsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?startkey=1790&endkey=1810{"total_rows":44,"offset":1,"rows":[! {"id":"...","key":1797,"value":"John Adams"},! {"id":"...","key":1801,"value":"Thomas Jefferson"},! {"id":"...","key":1809,"value":"James Madison"}]} 20
  • 44. Invoke a View 21
  • 45. Invoke a View 21
  • 46. Limit the Number of DocumentsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?limit=2 22
  • 47. Limit the Number of DocumentsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?limit=2{"total_rows":44,"offset":0,"rows":[! {"id":"...","key":1789,"value":"George Washington"},! {"id":"...","key":1797,"value":"John Adams"},]} 22
  • 48. Invoke a View 23
  • 49. Invoke a View 23
  • 50. presidents (_count) 24
  • 51. Invoke a View 25
  • 52. Reduce: _count{ "_id":"_design/design_doc", "_rev": "1-157b2928bec2def71485cc751af7de37", "views": { "presidents": { "map":"function(doc) { ! if(doc.type == president) { ! ! emit(doc.took_office, doc) }}", "reduce":"_count" } }, ...} 26
  • 53. _count FunctionGET http://localhost:5984/presidents/_design/design_doc/_view/presidents 27
  • 54. _count FunctionGET http://localhost:5984/presidents/_design/design_doc/_view/presidents{"rows":[! {"key":null,"value":44}]} 27
  • 55. Invoke a View 28
  • 56. Invoke a View 28
  • 57. total_time_in_office (_sum) 29
  • 58. Invoke a View 30
  • 59. A Map Function for _sum{ "_id":"_design/design_doc", "_rev": "1-157b2928bec2def71485cc751af7de37", "views": { total_time_in_office": { "map":"function(doc) { ! ! if(doc.type == president) { ! ! ! emit(doc.name, doc.left_office - doc.took_office) ! }}", "reduce":"_sum" } }, ...} 31
  • 60. A Map Function for _sum{ "_id":"_design/design_doc", "_rev": "1-157b2928bec2def71485cc751af7de37", "views": { total_time_in_office": { value is number of years in office "map":"function(doc) { ! ! if(doc.type == president) { ! ! ! emit(doc.name, doc.left_office - doc.took_office) ! }}", "reduce":"_sum" } }, will be sorted by name ...} 31
  • 61. Reduce: _sum{ "_id":"_design/design_doc", "_rev": "1-157b2928bec2def71485cc751af7de37", "views": { total_time_in_office": { "map":"function(doc) { ! ! if(doc.type == president) { ! ! ! emit(doc.name, doc.left_office - doc.took_office) ! }}", "reduce":"_sum" } }, ...} 32
  • 62. Reduce: _sum{ "_id":"_design/design_doc", "_rev": "1-157b2928bec2def71485cc751af7de37", "views": { total_time_in_office": { _sum requires number values "map":"function(doc) { ! ! if(doc.type == president) { ! ! ! emit(doc.name, doc.left_office - doc.took_office) ! }}", "reduce":"_sum" } }, ...} 32
  • 63. _sum FunctionGET http://localhost:5984/presidents/_design/design_doc/_view/total_time_in_office 33
  • 64. _sum FunctionGET http://localhost:5984/presidents/_design/design_doc/_view/total_time_in_office{"rows":[! {"key":null,"value":232}]} 33
  • 65. Invoke a View 34
  • 66. Invoke a View 34
  • 67. time_in_office (_stats) 35
  • 68. Invoke a View 36
  • 69. Reduce: _stats{ "_id":"_design/design_doc", "_rev": "1-157b2928bec2def71485cc751af7de37", "views": { "time_in_office": { "map":"function(doc) { ! ! if(doc.type == president) { ! ! ! emit(doc.name, doc.left_office - doc.took_office) ! }}", "reduce":"_stats" } }, ...} 37
  • 70. _stats FunctionGET http://localhost:5984/presidents/_design/design_doc/_view/time_in_office 38
  • 71. _stats FunctionGET http://localhost:5984/presidents/_design/design_doc/_view/time_in_office{"rows":[! {"key":null,! "value":{ ! "sum":232,"count":43,"min":0,"max":12,"sumsqr":1546 }]} 38
  • 72. Invoke a View 39
  • 73. Invoke a View 39
  • 74. View Trees 40
  • 75. Disk-Based View Tree k=size of interior node A-R n=number of keys A-H I-Rdepth= log k(n) A-C D-F G-H I-L N-R A B C D F G H I K L N O Q R leaves 41
  • 76. Disk-Based View Tree root k=size of interior node A-R n=number of keys A-H interior nodes I-Rdepth= log k(n) A-C D-F G-H I-L N-R A B C D F G H I K L N O Q R leaves 41
  • 77. _count Nodes root 14 7 reductions 7 3 2 2 3 4A B C D F G H I K L N O Q R keys 42
  • 78. _count Nodes root A-R 14 A-H 7 reductions I-R 7 A-C D-F G-H I-L N-R 3 2 2 3 4A B C D F G H I K L N O Q R keys 43
  • 79. Inserting a New Document A-R 14 A-H I-R 7 7 A-C D-F G-H I-L N-R 3 2 2 3 4A B C D F G H I K L M N O Q R 44
  • 80. Inserting a New Document A-R 14 A-H I-R 7 7 A-C D-F G-H I-L N-R 3 2 2 3 4A B C D F G H I K L M N O Q R new key 44
  • 81. Inserting a New Document A-R 14 A-H I-R 7 7 M-R 5 A-C D-F G-H I-L N-R 3 2 2 3 4A B C D F G H I K L M N O Q R new key 44
  • 82. Inserting a New Document A-R 14 I-R 8 A-H I-R 7 7 M-R 5 A-C D-F G-H I-L N-R 3 2 2 3 4A B C D F G H I K L M N O Q R new key 44
  • 83. Inserting a New Document A-R 15 A-R 14 I-R 8 A-H I-R 7 7 M-R 5 A-C D-F G-H I-L N-R 3 2 2 3 4A B C D F G H I K L M N O Q R new key 44
  • 84. Inserting a New Document new root A-R 15 A-R 14 new reductions I-R 8 A-H I-R 7 7 M-R 5 A-C D-F G-H I-L N-R 3 2 2 3 4A B C D F G H I K L M N O Q R new key 44
  • 85. Committing the Change A-R 15 A-R 14 I-R 8 A-H I-R 7 7 M-R 5 A-C D-F G-H I-L N-R 3 2 2 3 4A B C D F G H I K L M N O Q R 45
  • 86. Committing the Change A-R 15 14 A-H I-R 7 87 A-C D-F G-H I-L M-R N-R 3 2 2 3 5 4A B C D F G H I K L M N O Q R 45
  • 87. Getting a Key Range A-R 14 A-H I-R 7 7 A-C D-F G-H I-L M-R 3 2 2 3 5A B C D F G H I K L M N O Q R startkey endkey 46
  • 88. Key Range Reduction 15 7 8 3 2 2 3 5A B C D F G H I K L M N O Q R startkey endkey 47
  • 89. Key Range Reduction 15 (8) (3) 7 8 (5) 3 (1) 2 2 3 5 (2)A B C D F G H I K L M N O Q R startkey endkey 47
  • 90. More Ways to Use Views 48
  • 91. Skip the Reduce FunctionGET http://localhost:5984/presidents/_design/design_doc/_view/time_in_office?reduce=false 49
  • 92. Skip the Reduce FunctionGET http://localhost:5984/presidents/_design/design_doc/_view/time_in_office?reduce=false{"total_rows":44,"offset":0,"rows":[ {"id":"...","key":"Abraham Lincoln","value":5}, {"id":"...","key":"Andrew Jackson","value":8}, {"id":"...","key":"Andrew Johnson","value":4}, ...]} 49
  • 93. Invoke a View 50
  • 94. Invoke a View 50
  • 95. Reversing the Order of ResultsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?descending=true 51
  • 96. Reversing the Order of ResultsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?descending=true{"total_rows":44,"offset":0,"rows":[ {"id":"...","key":2009,"value":"Barack Obama"}, {"id":"...","key":2001,"value":"George W. Bush"}, {"id":"...","key":1993,"value":"Bill Clinton"}, {"id":"...","key":1989,"value":"George H. W. Bush"}, ...]} 51
  • 97. Invoke a View 52
  • 98. Invoke a View 52
  • 99. Reversing the Order of a RangeGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?descending=true&startkey=1850&endkey=1790 53
  • 100. Reversing the Order of a Range startkey and endkey are reversed, tooGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?descending=true&startkey=1850&endkey=1790 53
  • 101. Reversing the Order of a Range startkey and endkey are reversed, tooGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?descending=true&startkey=1850&endkey=1790{"total_rows":44,"offset":0,"rows":[ {"id":"...","key":2009,"value":"Barack Obama"}, {"id":"...","key":2001,"value":"George W. Bush"}, {"id":"...","key":1993,"value":"Bill Clinton"}, {"id":"...","key":1989,"value":"George H. W. Bush"}, ...]} 53
  • 102. Invoke a View 54
  • 103. Invoke a View 54
  • 104. Ignore a Given Number of RowsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?limit=10&skip=1 55
  • 105. Ignore a Given Number of RowsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?limit=10&skip=1 avoid large values 55
  • 106. Invoke a View 56
  • 107. Invoke a View 56
  • 108. Paginating (Initial Page)// first page of documentsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?limit=2 57
  • 109. Paginating (Initial Page)// first page of documentsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?limit=2{"total_rows":44,"offset":0,"rows":[! {"id":"...","key":1789,"value":"George Washington"},! {"id":"...","key":1797,"value":"John Adams"},]} last key of result 57
  • 110. Paginating (Successive Pages) 58
  • 111. Paginating (Successive Pages) last key of previous result// successive pagesGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?startkey=1797&skip=1&limit=2 dont include the first document 58
  • 112. Paginating (Successive Pages)// successive pagesGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?startkey=1797&skip=1&limit=2{"total_rows":44,"offset":2,"rows":[! {"id":"...","key":1801,"value":"Thomas Jefferson"},! {"id":"...","key":1809,"value":"James Madison"},]} 58
  • 113. Invoke a View 59
  • 114. Invoke a View 59
  • 115. Paginating in Reverse Order// first page of documentsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?descending=true&limit=2 60
  • 116. Paginating in Reverse Order// first page of documentsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?descending=true&limit=2// successive pages last key of previous resultGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?descending=true&startkey=1797&skip=1&limit=2 dont include the first document 60
  • 117. Invoke a View 61
  • 118. Invoke a View 61
  • 119. Using a Stale ViewGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?stale=ok 62
  • 120. Invoke a View 63
  • 121. Invoke a View 63
  • 122. Invoke a View 64
  • 123. Invoke a View 64
  • 124. Invoke a View 65
  • 125. Invoke a View 65
  • 126. Invoke a View 66
  • 127. Invoke a View 66
  • 128. Updating the View Immediately AfterGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?stale=update_after 67
  • 129. party_state_name(group & group_level) 68
  • 130. Invoke a View 69
  • 131. group_level=1Map Keys Group Level 1["a",1,1] {"key":["a"], "value":3}["a",3,4]["a",3,8]["b",2,6] {"key":["b"], "value":2}["b",2,6]["c",1,5] {"key":["c"], "value":2}["c",4,2] 70
  • 132. group_level=1 Map Keys Group Level 1 ["a",1,1] {"key":["a"], "value":3} ["a",3,4] ["a",3,8] ["b",2,6] {"key":["b"], "value":2} ["b",2,6] ["c",1,5] {"key":["c"], "value":2} ["c",4,2] GET http://localhost:5984/my_db/_design/ddoc/_view/v1? group_level=1only applies to reduce views 70
  • 133. group_level=2Map Keys Group Level 2["a",1,1] {"key":["a",1], "value":1}["a",3,4] {"key":["a",3], "value":2}["a",3,8]["b",2,6] {"key":["b",2], "value":2}["b",2,6]["c",1,5] {"key":["c",1], "value":1}["c",4,2] {"key":["c",4], "value":1} 71
  • 134. group_level=2 Map Keys Group Level 2 ["a",1,1] {"key":["a",1], "value":1} ["a",3,4] {"key":["a",3], "value":2} ["a",3,8] ["b",2,6] {"key":["b",2], "value":2} ["b",2,6] ["c",1,5] {"key":["c",1], "value":1} ["c",4,2] {"key":["c",4], "value":1} GET http://localhost:5984/my_db/_design/ddoc/_view/v1? group_level=2only applies to reduce views 71
  • 135. Invoke a View 72
  • 136. Group Level 73
  • 137. Group Level 73
  • 138. Group Level 74
  • 139. Group Level 74
  • 140. Group 75
  • 141. Group GET http://localhost:5984/my_db/_design/greeting/_view/v1? group=true one output row for each unique keyequivalent to group_level=infinity 75
  • 142. Group Level 76
  • 143. Group Level 76
  • 144. Including Full DocumentsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?include_docs=true 77
  • 145. Including Full DocumentsGET http://localhost:5984/presidents/_design/design_doc/_view/president_names?include_docs=true{"total_rows":44,"offset":0,"rows":[{"id":"...","key":1789,"value":"George Washington","doc":{"_id":"...","_rev":"1-...","presidency":"1","wikipedia_entry":"http://en.wikipedia.org/...","took_office":1789,"left_office":1797,"party":"Independent","home_state":"Virginia","name":"George Washington","type":"president"}},! ,...]} 77
  • 146. Group Level 78
  • 147. Group Level 78
  • 148. Emitting with include_docs=true 79
  • 149. Emitting with include_docs=true includes the latest rev of the emitterfunction(doc) { emit("key", aValue) } 79
  • 150. Emitting with include_docs=true includes the latest rev of the emitterfunction(doc) { emit("key", aValue) } includes this rev of the emitterfunction(doc) { emit("key",{"_rev":doc._rev; aValue}) } 79
  • 151. Emitting with include_docs=true includes the latest rev of the emitterfunction(doc) { emit("key", aValue) } includes this rev of the emitterfunction(doc) { emit("key",{"_rev":doc._rev; aValue}) } includes document with id foofunction(doc) { emit("key",{"_id":"foo", aValue}) } 79
  • 152. Requesting Specific Keys 80
  • 153. Requesting Specific KeysPOST -H "Content-Type:application/json"http://localhost:5984/presidents/_design/design_doc/_view/president_names-d {"keys":[1789, 1929, 1993, ... ]} 80
  • 154. Requesting Specific KeysPOST -H "Content-Type:application/json"http://localhost:5984/presidents/_design/design_doc/_view/president_names-d {"keys":[1789, 1929, 1993, ... ]}POST -H "Content-Type:application/json"http://localhost:5984/presidents/_design/design_doc/_view/president_names?include_docs=true-d {"keys":[1789, 1929, 1993, ... ]} 80
  • 155. Group Level 81
  • 156. Group Level 81
  • 157. president_events (join) 82
  • 158. Invoke a View 83
  • 159. Collating Joins"views": { "president_events": "function(doc) { if (doc.type == president) { emit([doc.took_office], doc.name); } else if (doc.type == event) { emit([doc.year, 0], doc.event); } }"} 84
  • 160. Collating Joins year took office"views": { "president_events": "function(doc) { if (doc.type == president) { emit([doc.took_office], doc.name); } else if (doc.type == event) { emit([doc.year, 0], doc.event); } }"} 84
  • 161. Collating Joins year took office"views": { one-element array "president_events": "function(doc) { if (doc.type == president) { emit([doc.took_office], doc.name); } else if (doc.type == event) { emit([doc.year, 0], doc.event); } }"} 84
  • 162. Collating Joins year took office"views": { one-element array "president_events": "function(doc) { if (doc.type == president) { emit([doc.took_office], doc.name); } else if (doc.type == event) { emit([doc.year, 0], doc.event); } }" year of event} 84
  • 163. Collating Joins year took office"views": { one-element array "president_events": "function(doc) { if (doc.type == president) { emit([doc.took_office], doc.name); } else if (doc.type == event) { emit([doc.year, 0], doc.event); } }" year of event} second array element 84
  • 164. Join Presidents and EventsGET http://localhost:5984/presidents/_design/design_doc/_view/president_events 85
  • 165. Join Presidents and EventsGET http://localhost:5984/presidents/_design/design_doc/_view/president_events{"total_rows":883,"offset":0,"rows":[ {"id":"...","key":[1789],"value":"George Washington"}, {"id":"...","key":[1790,0],"value":"Rhode Island ratifies the Constitution and becomes 13th state"}, {"id":"...","key":[1791,0],"value":"Bill of Rights ratified"}, {"id":"...","key":[1791,0],"value":"First Bank of the United States chartered"}, ...]} 85
  • 166. Group Level 86
  • 167. Group Level 86
  • 168. Viewshttp://alan.iriscouch.com/presidents/_design/design_docPresidents Filehttp://alan.iriscouch.com/presidents/_design/design_doc/presidents.jsonEvents Filehttp://alan.iriscouch.com/presidents/_design/design_doc/us_events.json 87
  • 169. Thanks! 88