I have some problems with mapreduce
.
I want to group, sort and count some values in collection. I have collection such as:
----------------------------
| item_id | date |
----------------------------
| 1 | 01/15/2012 |
----------------------------
| 2 | 01/01/2012 |
----------------------------
| 1 | 01/15/2012 |
----------------------------
| 1 | 01/01/2012 |
----------------------------
| 2 | 01/03/2012 |
----------------------------
| 2 | 01/03/2012 |
----------------------------
| 1 | 01/01/2012 |
----------------------------
| 1 | 01/01/2012 |
----------------------------
| 2 | 01/01/2012 |
----------------------------
| 2 | 01/01/2012 |
----------------------------
I want to group by item_id
and count date by day for each item and sort date for each item and get result such as:
value: {{item_id:1, date:{01/01/2012:3, 01/15/2012:2 }},{item_id:2, date:{01/01/2012:3, 01/03/2012:2 }}}
I use mapReduce
:
m=function()
{
emit(this.item_id, this.date);
}
r=function(key, values)
{
var res={};
values.forEach(function(v)
{
if(typeof res[v]!='undefined') ? res[v]+=1 : res[v]=1;
});
return res;
}
But I didn't receive result such as:
{{item_id:1, date:{01/01/2012:3, 01/15/2012:2 }},{item_id:2, date:{01/01/2012:3, 01/03/2012:2 }}}
Any ideas?