2015-11-22 51 views
1

我需要使用臨時變量將值臨時存儲在reduce函數中。在我的輸出日誌中,結果表明reduce函數運行的次數是0.但是,當我執行其他類型的計算時,它會運行。減少方法不在mongodb mapreduce中運行

當我執行代碼時,reduce函數不能訪問對象。如果它有任何幫助,我使用mongojs庫作爲nodejs。

var map = function() { 
     emit(this._id, 
      { 
      d1: this.day_chan1, 
      d2: this.day_chan2, 
      d3: this.day_chan3 
      }); 
    };  

var reduce = function(key, values) { 

         var data = {}; 

         var curr = values[0]; 

         data.d1 = curr.d1 - prev.d1; 
         data.d2 = curr.d2 - prev.d2; 
         data.d3 = curr.d3 - prev.d3; 

         prev = curr; 

         return { timestamp: key , data: data }; 

     }; 

var prev = { d1: 0, d2: 0, d3: 0 }; 

this 
    .db 
    .mapReduce(
        map, 
          reduce,             
          { 
          scope: { prev : prev }, 
          sort: { timestamp: 1 }, 
          query: {}, 
          out: 'temp'             
          } 
         , 
         function (err, data) {           
          if(err) callback(err); 
          else callback(data); 
         }); 

這是數據的樣本,我處理

{ 
    "_id" : ObjectId("5650845744aeea3dba90326d"), 
    "timestamp" : "2015-09-21 05:02:52", 
    "curr_property" : NumberInt(1818), 
    "curr_property_cost" : NumberInt(21), 
    "day_property" : NumberInt(30), 
    "day_property_cost" : NumberInt(18), 
    "curr_solar_generating" : NumberInt(676), 
    "curr_solar_export" : NumberInt(0), 
    "day_solar_generated" : NumberInt(11), 
    "day_solar_export" : NumberInt(0), 
    "curr_chan1" : NumberInt(676), 
    "curr_chan2" : NumberInt(676), 
    "curr_chan3" : NumberInt(466), 
    "day_chan1" : NumberInt(11), 
    "day_chan2" : NumberInt(11), 
    "day_chan3" : NumberInt(7), 
    "gatewayId" : 23.0 
} 
{ 
    "_id" : ObjectId("5650845744aeea3dba90326e"), 
    "timestamp" : "2015-09-21 05:03:52", 
    "curr_property" : NumberInt(1818), 
    "curr_property_cost" : NumberInt(21), 
    "day_property" : NumberInt(60), 
    "day_property_cost" : NumberInt(18), 
    "curr_solar_generating" : NumberInt(676), 
    "curr_solar_export" : NumberInt(0), 
    "day_solar_generated" : NumberInt(22), 
    "day_solar_export" : NumberInt(0), 
    "curr_chan1" : NumberInt(676), 
    "curr_chan2" : NumberInt(676), 
    "curr_chan3" : NumberInt(466), 
    "day_chan1" : NumberInt(22), 
    "day_chan2" : NumberInt(22), 
    "day_chan3" : NumberInt(15), 
    "gatewayId" : 23.0 
} 
{ 
    "_id" : ObjectId("5650845744aeea3dba90326f"), 
    "timestamp" : "2015-09-21 05:04:52", 
    "curr_property" : NumberInt(1818), 
    "curr_property_cost" : NumberInt(21), 
    "day_property" : NumberInt(91), 
    "day_property_cost" : NumberInt(19), 
    "curr_solar_generating" : NumberInt(676), 
    "curr_solar_export" : NumberInt(0), 
    "day_solar_generated" : NumberInt(33), 
    "day_solar_export" : NumberInt(0), 
    "curr_chan1" : NumberInt(676), 
    "curr_chan2" : NumberInt(676), 
    "curr_chan3" : NumberInt(466), 
    "day_chan1" : NumberInt(33), 
    "day_chan2" : NumberInt(33), 
    "day_chan3" : NumberInt(23), 
    "gatewayId" : 23.0 
} 

,這是我的日誌輸出

{ 
    "result" : "temp", 
    "timeMillis" : 3362, 
    "counts" : { 
     "input" : 39781, 
     "emit" : 39781, 
     "reduce" : 0, 
     "output" : 39781 
    }, 
    "ok" : 1 
} 

這是臨時收集的外觀在手術後

{ 
    "_id" : ObjectId("5650845744aeea3dba90326d"), 
    "value" : { 
     "d1" : 11.0, 
     "d2" : 11.0, 
     "d3" : 7.0 
    } 
} 
{ 
    "_id" : ObjectId("5650845744aeea3dba90326e"), 
    "value" : { 
     "d1" : 22.0, 
     "d2" : 22.0, 
     "d3" : 15.0 
    } 
} 
{ 
    "_id" : ObjectId("5650845744aeea3dba90326f"), 
    "value" : { 
     "d1" : 33.0, 
     "d2" : 33.0, 
     "d3" : 23.0 
    } 
} 

她e是我所希望得到的

{ 
    "_id" : ObjectId("5650845744aeea3dba90326d"), 
    "value" : { 
     "d1" : 0, 
     "d2" : 0, 
     "d3" : 0 
    } 
} 
{ 
    "_id" : ObjectId("5650845744aeea3dba90326e"), 
    "value" : { 
     "d1" : 11.0, 
     "d2" : 11.0, 
     "d3" : 9.0 
    } 
} 
{ 
    "_id" : ObjectId("5650845744aeea3dba90326f"), 
    "value" : { 
     "d1" : 11.0, 
     "d2" : 11.0, 
     "d3" : 18.0 
    } 
} 

我在這一塊上敲了敲頭,非常感謝一些幫助。

UPDATE:

代碼當前服務的方法在內部運行mongojs。這在mongo控制檯中是相當的。

db.testdb.mapReduce(map, 
        reduce,            
        { 
          scope: { prev : prev }, 
          sort: { timestamp: 1 }, 
          query: {}, 
          out: 'temp'             
        }); 

map和reduce方法的聲明與頂部的聲明相同。

+0

你能編輯你的問題,向我們展示以下三個元素,可能會增加你得到正確答案的機會;一些示例文檔,實際輸出和您的預期結果? – chridam

+0

@chridam剛更新了它。 – Bazinga777

+0

你可以放置prev高於reduce函數嗎? – Ravenous

回答