I think I need to write map / reduce functions.
Yes, for this you need to reduce the map. For some simple examples of map reduction, please see here .
In your particular case, you first need to change your expectation of output. The result of the display / reduction is a collection. The collection will look (in your case) something like this:
{ key : { 'brand' : 'bmw' }, value : 2 } { key : { 'model' : 'i5' }, value : 1 }
To generate this set, you need the "map" and "reduce" function. The map function will give a key and a value. The key is each params element, the value is the number 1. The reduce function takes a key and an array of values ββand returns only one value. Your question is basically the same as in this example on the MongoDB website :
map = function() { if (!this.params) { return; } for (index in this.params) { emit(this.params[index], 1); } } reduce = function(previous, current) { var count = 0; for (index in current) { count += current[index]; } return count; }
source share