If you are storing different document types within the same
bucket, then you may want to ensure that you generate views only
on a specific record type within the map()
phase. This can be achieved by using an if
statement to select the record.
For example, if you are storing blog 'posts' and 'comments' within the same bucket, then a view on the blog posts could be created using the following map:
function(doc, meta) { if (doc.title && doc.type && doc.date && doc.author && doc.type == 'post') { emit(doc.title, [doc.date, doc.author]); } }
The same solution can also be used if you want to create a view
over a specific range or value of documents while still allowing
specific querying structures. For example, to filter all the
records from the statistics logging system over a date range
that are of the type warning you could use the following
map() function:
function(doc, meta) { if (doc.logtype == 'error') { emit([doc.year, doc.mon, doc.day],null); } }
The same solution can also be used for specific complex query types. For example, all the recipes that can be cooked in under 30 minutes, made with a specific ingredient:
function(doc, meta) { if (doc.totaltime && doc.totaltime <= 20) { if (doc.ingredients) { for (i=0; i < doc.ingredients.length; i++) { if (doc.ingredients[i].ingredtext) { emit(doc.ingredients[i].ingredtext, null); } } } } }
The above function allows for much quicker and simpler selection
of recipes by using a query and the key
parameter, instead of having to work out the range that may be
required to select recipes when the cooking time and ingredients
are generated by the view.
These selections are application specific, but by producing different views for a range of appropriate values, for example 30, 60, or 90 minutes, recipe selection can be much easier at the expense of updating additional view indexes.