DovetailDB
Map/Reduce Tutorial

This brief example demonstrates the map and reduce options on the query() operation, which permit many kinds of aggregations to be performed on the server side. If you've not yet created any server-side javascript, first take a look at the introduction to server-side functions.

To get us started, let's define a few server-side javascript functions:

function one(o) { return 1; }
function add(x,y) { return x + y; }

Map/reduce systems are fairly simple; the map function get applied to each object, and subsequnetly, the reduce function is repeatedly called on pairs of results from the map (and from other reduces) until there is only a single result. The one() function we defined above can be used as a map function to turn each object into a count of one, and the add() function can be used as a reduce function to sum the counts. On the client side, we would issue such a count query like so:

dovetail.query("people", {age_min:18}, {map:"one",reduce:"add"}, function(results){
alert("We know "+results[0]+" people that are 18 or over.");
});