MapReduce for calculating parameter values

I have a document like this:

{ "_id": ObjectId("4d17c7963ffcf60c1100002f"), "title": "Text", "params": { "brand": "BMW", "model": "i3" } } { "_id": ObjectId("4d17c7963ffcf60c1100002f"), "title": "Text", "params": { "brand": "BMW", "model": "i5" } } 

I need an account of all parameter values. as:

 brand --------- BMW (2) model --------- i3 (1) i5 (1) 

I think I need to write map / reduce functions. How can i do this? Thanks.

+4
source share
2 answers

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; } 
+2
source

Your map function lists the properties of the params property of this . For each property, you will find an emit call with a key that contains both the name of the property and the value of the property. Pass 1 as a value. for example emit({'brand','BMW'}, 1) , but obviously using variables is not constant!

In your abbreviation function, you pass a key and an array of values. Sum these values ​​and return the amount. Despite the fact that the original array will be 1, you should not be tempted to use the length of the array, because the reduction function can be called iteratively.

After that, you can group the results from the results collection using the index, if necessary for performance.

+1
source

Source: https://habr.com/ru/post/1333442/


All Articles