Mongo 2.4:在Reduce阶段插入文档(mongo map red)

时间:2014-05-30 05:11:25

标签: mongodb

我们的意见是这样的

{ _id: 1,  deviceuuid: "d1",  sessiontime: 10, timestamp = 123456789123 }
{ _id: 2,  deviceuuid: "d1",  sessiontime: 13, timestamp = 123456789129 }
{ _id: 3,  deviceuuid: "d1",  sessiontime: 15, timestamp = 123456789133 }
{ _id: 4,  deviceuuid: "d1",  sessiontime: 16, timestamp = 123456789153 }
{ _id: 5,  deviceuuid: "d1",  sessiontime: 17, timestamp = 123456789163 }
{ _id: 6,  deviceuuid: "d2",  sessiontime: 10, timestamp = 123456789123 }
{ _id: 7,  deviceuuid: "d2",  sessiontime: 13, timestamp = 123456789129 }
{ _id: 8,  deviceuuid: "d2",  sessiontime: 15, timestamp = 123456789133 }
{ _id: 9,  deviceuuid: "d2",  sessiontime: 16, timestamp = 123456789153 }
{ _id: 10,  deviceuuid: "d2",  sessiontime: 17, timestamp = 123456789163 } 

我们累计收到“会话时间”事实。所以我们的要求是计算delta并放入不同的集合

在地图阶段:  我发出密钥:< deviceuuid>和值:{ sessiontime:<value>, timestamp=<value>}

在减少阶段: 我按时间戳排序值并计算delta

 e.g. for device uuid “d1”
10 -0 => 10
13 -10 => 3
15 -13 => 2
16 -15 => 1
17 -16 => 1 

我们需要在不同的集合中插入上面的数据,但db对象在reduce阶段不可用 即

db.result.save(..)  Giving error in reduce phase

请建议!!

1 个答案:

答案 0 :(得分:0)

在地图和减少阶段都不可能对任何集合进行任何修改。您唯一能做的就是将map-reduce的整个结果保存到另一个集合中。