Authors View
Finally, let’s save our authors view from Chapter 2 to
our new _design/default
design
document. In this view we will map book documents to key/value pairs of
authors and number of pages.
In Futon, select “Temporary view…” from the “View” drop-down menu, and paste the following function into the “Map Function” text box, replacing the existing function:
function(doc) { if (doc.authors) { for (var i in doc.authors) { emit(doc.authors[i], doc.pages); } } }
Enter the name of the built-in _stats
Reduce function in the “Reduce Function”
text box:
_stats
Run the temporary view if you’d like, and then click the “Save As…”
button. Enter default
again as the
“Design Document”, enter authors
as
the “View Name”, and then click the “Save” button. See Figure 3-5.
Figure 3-5. Saving the authors view in the default design document using Futon
Alternatively, you can update the default
design document to add the authors view
using cURL:
curl -X PUT http://localhost:5984/books/_design/default -d \ '{ "_id": "_design/default", "_rev": "2-cf88b785f94f2ddc1f9148f425f54f0d", "language": "javascript", "views": { "titles": { "map": "function(doc) { if (doc.title) { emit(doc.title, doc.pages); } }", "reduce": "_stats" }, "formats": { "map": "function(doc) { if (doc.formats) { for (var i in doc.formats) { emit(doc.formats[i], doc.pages); } } }", "reduce": "_stats" }, "authors": { "map": "function(doc) { ...
Get Writing and Querying MapReduce Views in CouchDB now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.