I canโt talk about MongoDB, but I can talk about CouchDB. CouchDB can only be called using the Map / Reduce View Engine. In fact, a great place to start is the wiki section .
The view contains the map function and an additional reduce function. JavaScript is a typical language for writing these functions, but there is an Erlang option, and you can build a viewing mechanism in almost any other programming language.
The map function is used to create a dataset from documents in the database. The reduction function aggregates this data set. Thus, the display function runs on each individual document in the database after creating the view. (and first request). After creation, this function is performed only in the document, which was either created or modified / deleted. Thus, presentation indices are built gradually , but not dynamically.
With 1,000,000,000 values, CouchDB will not need to calculate the results of your query every time it queries. Instead, it will only report the value of the index that it has saved, which itself changes only when creating / updating / deleting a document.
As for writing Map / Reduce functions, most of this work is left to the programmer, since there are no built-in map functions. (i.e. it is not "automatic"). However, there are several built-in reduction functions ( _sum , _count , _stats ).
Here is a simple example, we will calculate the average height of some people.
// sample documents { "_id": "Dominic Barnes", "height": 64 } { "_id": "Some Tall Guy", "height": 75 } { "_id": "Some Short(er) Guy", "height": 58 } // map function function (doc) { // first param is "key", which we do not need since `_id` is stored anyways emit(null, doc.height); } // reduce function _stats
The results of this view will look like this:
{ "rows": [ { "key": null "value": { "sum": 197, "count": 3, "min": 58, "max": 75, "sumsqr": 13085 } } ] }
Calculating the average from here is as simple as dividing the amount into the account. If you want the average to be calculated in the view itself, you can check out this example .