{
doctype: "mobie",
year: 2002,
rating: 3
}
{
doctype: "mobie",
year: 2002,
rating: 1
}
{
doctype: "mobie",
year: 2003,
rating: 2
}
Every document gets run through the map function, in this case, only documents that are JSON and have the doctype of mobie (your application code set the doctype property for that document (likely your class name)).
function(doc, meta) {
if (meta.type == "json" && doc.doctype == "mobie") {
emit([year, rating], null)
}
}
The resulting Index (like a table) and the key of the document this came from. You are only indexing specific properties here, so you can "get" the document to retrieve the full document/object based on your query results keys.
1: [2002,1] (mobie::002)
2: [2002,3] (mobie::001)
3: [2003,2] (mobie::003)
If you notice in a compound index key, it is ordered by the elements of the array. Now you can query that View (Map/Reduce), with a range query by using a startkey and endkey, and because it's ordered, that's how you accomplish the mobie.year == xxxx and mobie.rating > 2
startkey=[2002,2]
endkey=[2002,5]
In this case since the range is from 2002 to 2002, and rating is from 2 to 5 (or > 2 basically if you change 5 to 9999 or whatever you want) you return only one result from the examples.
[2002,3] (mobie::001)
If we change the range to this:
startkey=[2002,2]
endkey=[2003,5]
We will get these results for that query:
[2002,3] (mobie::001)
[2003,2] (mobie::003)