根据Mapreduce Aggregation的结果值创建文档

时间:2015-01-02 12:42:45

标签: mongodb dictionary mapreduce mongodb-query reduce

我写了一个Map / Reduce程序,它返回(显然)一组键/值文档:

output { 
    _id: blabla,
    value: {
         ga: blabl,
         bu: blab,
         zo: bla,
         meu: bl
    }
}

但我最终想要的是以下集合,删除该值,并检索它的元素以创建新文档,更易于操作,更类似于聚合的结果:

collection {
   ga: blabl,
   bu: blab,
   zo: bla,
   meu: bl
}

在地图缩小过程中有没有办法做到这一点?我想这是一个常见的问题,但我找不到任何解释。

=============================================== ========================

讨论之后:为什么我尝试使用Map / Reduce,并认为聚合不适合:

更准确地说,这是我的数据的例子:

events collection 
{
   recipient_id: recip1,       //my aggregation key
   EventType: type1,           //50 distinct values
   EventOrganizer: orgaName1,  //10 distinct values
   date_sent: jj/mm/aaaa,      //always provided
   date_action1: jj/mm/aaaa,   //may be null
   date_action2: jj/mm/aaaa    //may be null
},
{
   recipient_id: recip2, 
   EventType: type1, 
   EventOrganizer: orgaName1, 
   date_sent: jj/mm/aaaa, 
   date_action1: jj/mm/aaaa, 
   date_action2: jj/mm/aaaa
},
{
   recipient_id: recip1, 
   EventType: type2, 
   EventOrganizer: orgaName1, 
   date_sent: jj/mm/aaaa, 
   date_action1: jj/mm/aaaa, 
   date_action2: jj/mm/aaaa
},etc.

我想要的是什么:

contacts collection 
{
   recipient_id: recip1,       //my aggregation key
   distinctNbOfEvents:
   distinctNbOfOrganizers:
   listDates:[jj//mm/aaaa,jj/mm/aaaa], //sorted list of each date_sent
   organizers:{
        orgaName1: nb_events, 
        orgaName2: nb_events,
        ...
        orgaNamek: nb_events
   }                          //for each organizer, the number of events organized for this recipient
   hasAction1:                //boolean, is there at least 1 event with a date_action1 not null
   hasAction2:                //boolean, is there at least 1 event with a date_action2 not null
   nbOfEvents:
   rateOfAction1:             //(nb of date_action1 not null)/(nb of events)
   rateOfAction2:             //(nb of date_action2 not null)/(nb of date_action1 not null)   
   avgDelay1:                 //between an event date_sent and the related action 1 if not null
   avgDelay2:                 //between an event date_sent and the related action 2 if not null
   sentFrequency:             //Avg number of days between the date_sent of two consecutives events, can be calculated as (more recent - more ancient)/(nbOfEvents -1)
}

我认为我无法对聚合:listDates,组织者,sentFrequency。

0 个答案:

没有答案